سؤال

I am trying to send some data to a generic handler and pass that information back in the response using jQuery.ajax(). For some reason, when I send the data up based on an answer to another question I found (.NET Simple Form Submit via AJAX and JQUERY), there is nothing in the context.Request object.

Here is my ajax call:

function retrieveStats(monster) {
    $.ajax({
        type: "POST",
        url: "MonsterRequests.ashx",
        data: { "monster": monster },
        contentType: "application/json; charset=utf-8",
        success: function (msg) {
            alert(msg.d);
        },
        error: function (jqXhr, status, errorThrown) {
            alert(errorThrown);
        }
    });
}

And here is the code for my handler:

public class MonsterRequests : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        string monsterName = context.Request["monster"];

        context.Response.ContentType = "text/plain";
        context.Response.Write("{\"d\":\"" + monsterName + "\"}");
    }

    public bool IsReusable {
        get {
            return false;
        }
    }
}

I am able to pull the information off by accessing the context.Request.InputStream and reading via a StreamReader, but I am curious as to why I can't just pull the info directly off of the request.

Any help with this would be greatly appreciated.

هل كانت مفيدة؟

المحلول

Remove content type as you are not sending json. Try this.

var monster = "value";
    $.ajax({
        type: "POST",
        url: "MyHandler.ashx",
        data: { monster: monster },
        success: function(msg) {
            alert(msg.d);
        },
        error: function(jqXhr, status, errorThrown) {
            alert(errorThrown);
        }
    });
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top