jquery - 无法在 JQuery 1.6.4 中使用 CORS 进行 PUT/POST/DELETE HTTP 调用

标签 jquery http put cors

因此,我可以使用 CORS 成功地对我的服务进行 GET 调用。但是,在 POST、PUT 和 DELETE 操作的预检级别一定有问题。但是,据我所知,我的服务器为响应 OPTIONS 查询而返回的 header 响应是正确的,并且与

中描述的相匹配

这是我的 javascript 代码,在 JQuery 1.6.4 中使用 $.ajax。

$.ajax({
  url: 'http://myhome:8080/TaskApproval/resources/tasks/2',
  context: this,
  data: '<?xml version="1.0" encoding="UTF-8"?> <task> <description>Get carrots from the grocery store</description><originator>Chris</originator><subject>Get Carrots !!</subject><taskId>2</taskId> </task> ',
  timeout: 30000,
  type: 'PUT',
  contentType: 'application/xml',
  success: function(response) {
    alert(response);
    result = response;
  },
  error: function(xhr) {
    alert('Error!  Status = ' + xhr.status + " Message = " + xhr.statusText);
  }
});

现在,这是我的 HTTP Trail 的样子,由 Firebug 提供。

要求:

OPTIONS /TaskApproval/resources/tasks/2 HTTP/1.1
Host: widgethome:8080
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Origin: http://localhost:8080
Access-Control-Request-Method: PUT
Access-Control-Request-Headers: content-type

响应:

HTTP/1.1 200 OK
X-Powered-By: Servlet/3.0
Server: GlassFish v3
Allow: OPTIONS,GET,DELETE,HEAD,PUT, POST
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE
Access-Control-Max-Age: 1000
Access-Control-Allow-Headers: *
Content-Type: application/xml
Content-Length: 2792
Date: Wed, 28 Sep 2011 18:21:11 GMT

然后就没有 PUT(或 POST 或 DELETE)了,我只是得到了那个烦人的无用的 xhr 对象,看起来像这样:

readyState  0 
responseText    ""
status  0
statusText  "error"

我很困惑,如果我随后在我的 Ajax 调用中删除了 contentType,并且它根据我的应用程序发送了一个无效的内容类型,浏览器实际上发送了我的 PUT 请求,该请求失败了,因为 Content-Type 不是应用程序/xml。见下文:

$.ajax({
  url: 'http://myhome:8080/TaskApproval/resources/tasks/2',
  data: '<?xml version="1.0" encoding="UTF-8"?> <task> <description>Get carrots from the grocery store</description><originator>Chris</originator><subject>Get Carrots !!</subject><taskId>2</taskId> </task> ',
  timeout: 30000,
  type: 'PUT',
  //contentType: 'application/xml',
  success: function(response) {
    alert(response);
    result = response;
  },
  error: function(xhr) {
    alert('Error!  Status = ' + xhr.status + " Message = " + xhr.statusText);
  }
});

指向这个 HTTP Trail,再次由 Firebug 提供:

选项请求:

OPTIONS /TaskApproval/resources/tasks/2 HTTP/1.1
Host: myhome:8080
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Origin: http://localhost:8080
Access-Control-Request-Method: PUT

选项响应:

HTTP/1.1 200 OK
X-Powered-By: Servlet/3.0
Server: GlassFish v3
Allow: OPTIONS,GET,DELETE,HEAD,PUT, POST
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE
Access-Control-Max-Age: 1000
Access-Control-Allow-Headers: *
Content-Type: application/xml
Content-Length: 2792
Date: Wed, 28 Sep 2011 18:26:23 GMT

提交请求:

PUT /TaskApproval/resources/tasks/2 HTTP/1.1
Host: myhome:8080
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2
Accept: */*
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://localhost:8080/TaskApproval/crossdomain.html
Content-Length: 197
Origin: http://localhost:8080

放置响应:

HTTP/1.1 415 Unsupported Media Type
X-Powered-By: Servlet/3.0
Server: GlassFish v3
Content-Type: text/html
Content-Length: 1069
Date: Wed, 28 Sep 2011 18:26:23 GMT

415 是有道理的,因为我不支持内容 application/x-www-form-urlencoded,只支持 application/xml。我不明白的是为什么正确设置 Content-Type 会阻止 PUT?

感谢任何见解!我已经在互联网上搜索了很长一段时间,但找不到解决此问题的方法。

最佳答案

您需要在预检和实际响应中包含 CORS header 。因此,请尝试在服务器的 PUT 响应中包含以下 header :

Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE
Access-Control-Allow-Headers: Content-Type

另一件需要注意的事情是 CORS 规范没有将“*”列为 Access-Control-Allow-Headers 的有效值:

http://www.w3.org/TR/cors/#access-control-allow-headers-response-he

相反,您应该尝试像这样显式列出所有请求 header :

Access-Control-Allow-Headers: Content-Type

您必须包含 Content-Type,因为当 Content-Type 的值不是 application/x-www-form-urlencoded、multipart/form-data 或 text/plain(请参阅 CORS 规范有关简单 header 的更多详细信息)。

关于jquery - 无法在 JQuery 1.6.4 中使用 CORS 进行 PUT/POST/DELETE HTTP 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7587812/

相关文章:

javascript - 悬停时 Bootstrap 下拉,但主下拉仍然是一个链接

IIS 中托管的 WCF REST 服务不支持 PUT 和 DELETE

javascript - 内部订阅的Angular 6等待方法

http - 使用go http client Do方法时,httpResponse和error不能同时为nil吗?

javascript - 将数据从表更新到数据库?

c++ - 在 C++ 中使用 setInfo() 会导致 E_ADS_INVALID_USER_OBJECT

javascript - 将 JSON 对象传递给 onclick 函数

javascript - jQuery Mobile - 可折叠标题中的额外图标

javascript - 在页面加载和窗口调整大小时使用图像调整 div 大小

php - 从 javascript 和 PHP 上下文发布数据时使用相同的 PHP 类很困难