仅在 IE 中使用 jQuery 发布数据时出现错误请求错误

Bad Request error when posting data using jQuery in IE only

本文关键字:错误 数据 请求 IE jQuery 布数据 仅在      更新时间:2023-09-26

我用Knockout.js和Sammy.js编写了一个小型的单页应用程序,以及用于将数据发布回服务器的jQuery。

这在 Firefox 中工作正常,但我所有的 POST 在 IE 中都收到"400 错误请求"响应。

我已经使用Fiddler来查看发生了什么,我可以看到的唯一区别是IE在Referer标头中包含哈希,但我认为这不应该引起问题。

这是我的jQuery帖子:

$.ajax({
type: 'POST',
url: '/App_Presentation/BDM/RdmUserCategoryService.svc/GetUserCategoryUsersByCategory',
data: '{ "categoryId" : "' + this.params.catId + '" }',
contentType: 'application/json; charset=utf=8',
dataType: 'json',
success: function (data) {
    self.editCategoryData({
        CategoryID: ko.observable(categoryId),
        CategoryName: ko.observable(categoryName),
        UserList: ko.observableArray([])
    });
    self.editCategoryData().UserList(data.d);
}});

服务器上的方法不会被命中,成功回调也不会命中。当我添加错误回调时,我得到的唯一信息是"错误请求"错误消息。

有人有什么想法吗?

因此,

查看此内容的其他人都可以尝试解决此问题。尝试从contentType中删除charset。它适用于上述问题。

这可能与两个浏览器之间的轻微编码差异有关,WCF 可能会拒绝传入的请求,因为请求的编码实际上与contentType中指定的编码不匹配。虽然这不是一个确定的可靠答案。这是我拥有的最好的。

最终代码:

$.ajax({
type: 'POST',
url: '/App_Presentation/BDM/RdmUserCategoryService.svc/GetUserCategoryUsersByCategory',
data: '{ "categoryId" : "' + this.params.catId + '" }',
contentType: 'application/json'
dataType: 'json',
success: function (data) {
    self.editCategoryData({
        CategoryID: ko.observable(categoryId),
        CategoryName: ko.observable(categoryName),
        UserList: ko.observableArray([])
    });
    self.editCategoryData().UserList(data.d);
}});