AJAX post请求不会发送JSON数据
在过去的几个小时里,我一直在努力调试这个问题,最后我决定寻求帮助。
我有这样的数据,我想发送到 ashx 处理程序(这是大量数据)。
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"],"block":["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"]}]
我正在使用这个函数来发送请求:
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) {
}
});
};
在DataHandler.ashx中,这是相关代码:
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"];
}
}
通过调试,我发现DataHander甚至没有被调用。如果我从查询字符串中删除 value
,如下所示:
data: { key: key, action: "store" },
ProcessRequest 方法将按照我的预期被调用。
我猜测 value
可能太长了或者什么的。为什么没有发送,我该如何解决?
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"],"block":["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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当我运行测试代码时,我看到 jQuery ajax 调用返回以下错误:
所以你的查询字符串太长(至少对于我正在测试的 IE9 来说)。
正如评论所建议的,将其更改为 POST 允许在您的 ASHX 文件中访问 ProcessRequest 方法。
您还想更改 ProcessRequest 以从请求正文检索值,而不是查询字符串......
我希望这有帮助!
When I run my test code, I see the following error come back from the jQuery ajax call:
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....
I hope this helps!