在过去几个小时试图调试之后,我的头靠在墙上,我终于决定寻求帮助了。
我有这样的数据,我想发送给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
可能太长或者什么的。为什么不发送,我该如何解决?
答案 0 :(得分:1)
当我运行我的测试代码时,我看到jQuery ajax调用返回以下错误:
异常详细信息:System.Web.HttpException:长度 此请求的查询字符串 超过配置 maxQueryStringLength值。
所以你的查询字符串太长了(至少对我正在测试的IE9而言)。
正如评论所建议的那样,将其更改为POST允许在ASHX文件中访问ProcessRequest方法。
您还希望更改ProcessRequest以从请求正文中检索值,而不是查询字符串....
public void ProcessRequest(HttpContext context)
{
var query = context.Request;
string action = query["action"];
string key = query["key"];
string val = query["value"];
}
我希望这有帮助!