请问jQuery的AJAX调用支持补丁? [英] Does the jQuery ajax call support PATCH?

查看:219
本文介绍了请问jQuery的AJAX调用支持补丁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我把这个AJAX rquest:

  $。阿贾克斯({
            标题:{
                接受:应用/ JSON的,
                内容类型:应用/ JSON
            },
            网址:HTTP://本地主机:8080 / wutup /场地/ 12',
            类型:PATCH,
            数据:JSON.stringify({介绍:842490812321309213801923 gonzagazors}),
            成功:函数(响应,textStatus,jqXhr){
                的console.log(场地成功修补!);
            },
            错误:函数(jqXHR,textStatus,errorThrown){
                //错误记录到控制台
                的console.log(出现下列错误:+ textStatus,errorThrown);
            },
            完成:函数(){
                的console.log(场地补丁然);
            }
        });
 

我收到此错误:

XMLHtt prequest无法加载的http://本地主机:8080 / wutup /场地/ 12。法补丁不受访问控制 - 允许 - 方法允许的。

然而,使用curl:

  $卷曲-v -H接受:应用/ JSON-H内容类型:应用程序/ JSON-X PATCH  -  D'{地址:8421冈萨加大道}'的http://本地主机:8080 / wutup /场地/ 12

关于连接()到本地主机端口8080(#0)
尝试127.0.0.1 ...连接
连接到本地主机(127.0.0.1)端口8080(#0)
PATCH / wutup /场地/ 12 HTTP / 1.1
用户代理:卷曲/ 7.21.1(的i686-PC-的mingw32)的libcurl / 7.21.1的OpenSSL / 0.9.8r的zlib / 1.2.3
主持人:本地主机:8080
接受:应用/ JSON
内容类型:应用程序/ JSON
内容长度:57

HTTP / 1.1 204无内容
服务器:Apache-狼/ 1.1
访问控制 - 允许 - 产地:*
日期:星期五,2012 8点14分35秒格林尼治标准​​时间11月30日

连接#0到主机本地主机原封不动
关闭连接#0
 

解决方案

$。阿贾克斯方法的确实的支持HTTP PATCH。

您所看到的问题是, AJAX 方法查找PATCH在访问控制 - 允许 - 方法选项 $ P $响应头pflight检查。无论这个头是从你的反应缺失,或打补丁方法是不包括在这个头的值。在这两种情况下,这个问题是在服务器中,而不是在客户机侧code

下面是一个使用Java的一个例子:

  response.addHeader(访问控制 - 允许 - 方法,GET,POST,PATCH,PUT,DELETE);
 

When I send this ajax rquest:

$.ajax({
            headers : {
                'Accept' : 'application/json',
                'Content-Type' : 'application/json'
            },
            url : 'http://localhost:8080/wutup/venues/12',
            type : 'PATCH',
            data : JSON.stringify({description: "842490812321309213801923 gonzagazors"}),
            success : function(response, textStatus, jqXhr) {
                console.log("Venue Successfully Patched!");
            },
            error : function(jqXHR, textStatus, errorThrown) {
                // log the error to the console
                console.log("The following error occured: " + textStatus, errorThrown);
            },
            complete : function() {
                console.log("Venue Patch Ran");
            }
        });

I receive this error:

XMLHttpRequest cannot load http ://localhost:8080/wutup/venues/12. Method PATCH is not allowed by Access-Control-Allow-Methods.

However, using curl:

 $ curl -v -H "Accept: application/json" -H "Content-type: application/json" -X PATCH -    d' {"address": "8421 Gonzaga Ave"}'  http://localhost:8080/wutup/venues/12 

About to connect() to localhost port 8080 (#0)
Trying 127.0.0.1... connected
Connected to localhost (127.0.0.1) port 8080 (#0)
PATCH /wutup/venues/12 HTTP/1.1
User-Agent: curl/7.21.1 (i686-pc-mingw32) libcurl/7.21.1 OpenSSL/0.9.8r zlib/1.2.3
Host: localhost:8080
Accept: application/json
Content-type: application/json
Content-Length: 57

HTTP/1.1 204 No Content
Server: Apache-Coyote/1.1
Access-Control-Allow-Origin: *
Date: Fri, 30 Nov 2012 08:14:35 GMT

Connection #0 to host localhost left intact
Closing connection #0

解决方案

The $.ajax method does support HTTP PATCH.

The problem you are seeing is that the ajax method looks for PATCH in the Access-Control-Allow-Methods response header of the options preflight check. Either this header is missing from your response, or the PATCH method was not included in the value of this header. In either case, the problem is in the server, not in your client-side code.

Here's an example using Java:

response.addHeader("Access-Control-Allow-Methods", "GET, POST, PATCH, PUT, DELETE");

这篇关于请问jQuery的AJAX调用支持补丁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆