문제

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