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

标签 javascript jquery wcf knockout.js sammy.js

我使用 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/

相关文章:

javascript - Javascript和服务器之间的通信

javascript - 将字符串转换为嵌套数组

javascript - 如何在单击按钮时获取最接近类的 td 值?

javascript - 向 wcfrestful 发送一个参数

wcf - 在 Azure 上的 WCF 中通过 SSL 配置 webHTTP 和 NetHTTP 绑定(bind)

javascript - 使用 javascript 访问其他框架集中的前一个框架集 ID

javascript - 帮助自动调整背景图像的大小

javascript - jquery ajax 发布到自己不起作用

jquery - 将背景与视差滚动相结合

WCF 验证错误