【问题标题】:AJAX post request will not send JSON dataAJAX 发布请求不会发送 JSON 数据
【发布时间】:2011-06-07 18:07:37
【问题描述】:

在过去几个小时试图调试这个问题之后,我终于决定寻求帮助。

我有这样的数据,我想发送到 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 可能太长了。为什么没有发送,我该如何解决?

【问题讨论】:

  • 您是否尝试过通过 Fiddler 或 Firebug 监控您的请求?此外,将其设为 GET 可能会导致您遇到 URI + 查询字符串的最大长度,我认为这是 2000-4000 字符限制。我认为 IE8 有 2048 个限制。如果你通过 Fiddler 运行你的请求,你看到它被发送了吗?如果是这样,您在响应中看到了什么错误(如果有)?
  • 为什么你没有在$.ajax 中添加type: "POST"?其他方式,我认为它确实 GET 请求,并且必须在一个字符串中给出数据,如下所示:var=val&var2=val2 等等......
  • @David 它有一个400 Bad Request 错误。
  • 尝试将方法更改为 POST 看看会发生什么。如果这可行,您可能已达到 GET 长度限制。
  • @David 更改为 POST 并将 context.Request.QueryString 更改为 context.Request.Form 已修复,谢谢。你能发布一个我可以接受的答案吗?

标签: jquery ajax json ashx


【解决方案1】:

当我运行测试代码时,我看到 jQuery ajax 调用返回以下错误:

异常详情:System.Web.HttpException:长度 此请求的查询字符串 超出配置 maxQueryStringLength 值。

所以您的查询字符串太长(至少对于我正在测试的 IE9 而言)。

正如 cmets 建议的那样,将其更改为 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"];
}

我希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-11
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 2011-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多