我使用 Knockout.js 和 Sammy.js 编写了一个小型单页应用程序,并使用 jQuery 将数据发送回服务器。
这在 Firefox 中工作正常,但我的所有 POST 在 IE 中都收到“400 Bad Request”响应。
我使用 Fiddler 来查看发生了什么,我能看到的唯一区别是 IE 在 Referer header 中包含哈希,但我认为这不会导致问题。
这是我的 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
。它解决了上面的问题。
这可能与两个浏览器之间的细微编码差异有关,鉴于请求的编码实际上与 contentType
中指定的编码不匹配,WCF 可能会拒绝传入请求。虽然这并不是一个可靠的答案。这是我拥有的最好的。
最终代码:
$.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);
}});
关于javascript - 仅在 IE 中使用 jQuery 发布数据时出现错误请求错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20664341/