【问题标题】:jQuery .ajax PUT request not respecting custom headerjQuery .ajax PUT 请求不尊重自定义标头
【发布时间】:2012-12-04 04:00:19
【问题描述】:

将 PUT 类型与 .ajax 一起使用时,我的自定义标题丢失了。但是,标头可以使用 GET,但会被 PUT 破坏。请参阅证据 1:

// 好的获取:

$.ajax({
    url: url,
    type: 'GET',
    dataType: 'json',
    headers: {
        Accept: "application/json"
    }
});

// Actual header sent (using fiddler):
Accept: application/json

// 错误放置:

$.ajax({
    url: url,
    type: 'PUT',
    dataType: 'json',
    headers: {
        Accept: "application/json"
    }
});

// Actual header sent (using fiddler):
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

如您所见,唯一的区别是类型的值导致标头的 Accept 部分的值被丢弃。 jquery-1.8.2.js。有什么想法吗?谢谢

刺伤

【问题讨论】:

  • 来自 jQuery API 文档:The type of request to make ("POST" or "GET"), default is "GET". Note: Other HTTP request methods, such as PUT and DELETE, can also be used here, but they are not supported by all browsers.
  • 我在他们的文档中看到了这一点,但我希望 webkit 或 FF 可以与 PUT 一起使用,但他们没有。这让我相信这是 jquery 的问题。 .ajax 不设置标题。我今天将使用普通的 xhr 请求进行测试
  • 我会“期望”这些浏览器也能正常工作,但我们真正知道什么?同时,它很有可能是 jQuery :)

标签: javascript jquery http-headers


【解决方案1】:

好像和浏览器有关:

http://jsfiddle.net/oceog/WqXzA/

Request URL:http://fiddle.jshell.net/_display/
Request Method:PUT
Status Code:200 OK
Request Headersview source
Accept:application/json

Chrome 25.0

【讨论】:

  • 事实上,我确实在 Firefox 和 chrome 中获得了不同的 Accept 标头。 Chrome 给了我 /。这是否意味着在使用 PUT 时我基本上无法控制接受标头?这是一颗难以下咽的药丸
猜你喜欢
  • 2017-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 2021-10-31
  • 1970-01-01
  • 2016-12-31
相关资源
最近更新 更多