开发者

AJAX post request will not send JSON data

After banging my head against the wall over this for the past few hours trying to debug this, I have finally resolved to asking for help.

I have this data like this I am wanting to send to an ashx handler (it's a good deal of data).

var value = [{"start":["3,0"],"block":["0,0","1,2"],"end":["2,1"],"star":"gold","moves":3,"difficulty":"easy"},{"start":["1,0"],"block":["1,3","3,0","4,2"],"end":["0,1"],"star":"gold","moves":4,"difficulty":"easy"},{"start":["3,0"],"block":["0,0","0,2","2,0","3,2"],"end":["1,0"],"star":"silver","moves":4,"difficulty":"easy"},{"start":["3,0"],"block":["0,0","2,0","3,1"],"end":["1,3"],"star":"gold","moves":6,"difficulty":"easy"},{"start":["0,0","2,0"],"block":["2,3"],"end":["1,2"],"star":"gold","moves":4,"difficulty":"easy"},{"start":["2,1"],"block":["0,1","0,2","1,0","1,1","2,0","2,2","2,3","3,1","3,2"],"end":["1,3"],"star":"gold","moves":5,"difficulty":"easy"},{"start":["1,0"],"block":["0,0","3,0","2,3"],"end":["4,1"],"star":"gold","moves":5,"difficulty":"medium"},{"start":["0,0","0,4"],"block":["0,5","0,2","3,3"],"end":["1,1"],"star":"gold","moves":7,"difficulty":"medium"},{"start":["0,0","2,6"],"block":["0,5","3,3","2,1"],"end":["3,5"],"star":"gold","moves":8,"difficulty":"medium"},{"start":["4,1","4,3"],"bl开发者_Go百科ock":["3,0","4,2"],"end":["0,1","1,4","3,2"],"star":"gold","moves":8,"difficulty":"medium"},{"start":["1,2","3,4","4,2"],"block":["0,2","3,0"],"end":["2,3"],"star":"gold","moves":9,"difficulty":"medium"},{"start":["3,1","3,6"],"block":["0,0","0,3","0,7","2,5"],"end":["2,3"],"star":"gold","moves":11,"difficulty":"hard"},{"start":["0,7","0,2"],"block":["2,0","3,2","0,6","1,6","1,7"],"end":["3,3"],"star":"gold","moves":12,"difficulty":"hard"},{"start":["0,0","0,3"],"block":["0,1","2,2","3,0","3,3"],"end":["4,2"],"star":"gold","moves":8,"difficulty":"hard"},{"start":["0,0","0,6"],"block":["0,1","1,0","1,1","2,5","3,7"],"end":["3,4"],"star":"gold","moves":13,"difficulty":"hard"},{"start":["0,0","0,2","0,4","2,0","2,4","3,2","4,0","4,4"],"block":["0,1","0,3","1,0","1,1","1,2","1,3","1,4","2,1","2,3","3,0","3,1","3,3","3,4","4,1","4,2","4,3"],"end":["2,2"]},{"start":["0,0","0,2","0,4","1,1","2,0","2,4","3,2","4,0","4,2","4,4"],"block":["0,1","0,3","1,0","1,2","1,3","1,4","2,1","2,3","3,0","3,1","3,3","3,4","4,1","4,3"],"end":["2,2"],"star":"silver","moves":42,"difficulty":"medium"},{"start":["0,0","3,3","4,0"],"block":["0,1","2,3","3,0","4,4"],"end":["0,3"],"star":"gold","moves":11,"difficulty":"hard"},{"start":["0,4","1,1","3,5","4,2"],"block":["0,0","3,1","4,1"],"end":["2,3"],"star":"gold","moves":14,"difficulty":"hard"},{"start":["0,0","3,2","3,6"],"block":["0,4","0,5","4,4"],"end":["1,1"],"star":"gold","moves":13,"difficulty":"hard"},{"start":["0,2"],"block":["0,7","4,0","4,6","5,0","6,0","6,5"],"end":["2,0"]}]

And I am using this function to send the request:

function storeValue(value) {
    var val = encodeURIComponent(JSON.stringify(value));
    $.ajax({
        url: "DataHandler.ashx",
        async: false,
        data: { key: "someKey", value: val, action: "store" },
        datatype: "json",
        success: function (data) {
        }
    });
};

In the DataHandler.ashx, this is the relevant code:

public class DataHandler : IHttpHandler, IReadOnlySessionState
    {
        public void ProcessRequest(HttpContext context)
        {
             var query = context.Request.QueryString;
             string action = query["action"];
             string key = query["key"];
             string val = query["value"];
        }
    }

Through debugging, I find out that the DataHander isn't even being called. If I remove the value from the query string, like this:

data: { key: key, action: "store" },

The ProcessRequest method will be called as I would expect.

I am guessing that value might be too long or something. Why isn't it being sent, and how can I fix it?


When I run my test code, I see the following error come back from the jQuery ajax call:

Exception Details: System.Web.HttpException: The length of the query string for this request exceeds the configured maxQueryStringLength value.

So your query string is too long (at least for IE9 which is what I'm testing on).

As the comments suggested, changing this to a POST allows the ProcessRequest method to be reached in your ASHX file.

You'd also want to change ProcessRequest to retrieve values from the request body, not the query string....

public void ProcessRequest(HttpContext context)
{
    var query = context.Request;
    string action = query["action"];
    string key = query["key"];
    string val = query["value"];
}

I hope this helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜