$ .ajax与type:post和$ .post之间的区别是什么?

时间:2022-06-21 15:49:23

Consider this code:

考虑以下代码:

$.ajax({
           url: "http://x.com/api/AnnouncementCategory/Save",
           type: "Post",
           success: function (data) {
               //Grab our data from Ground Control
               alert(data);
           },
           error: function (event) {
               //If any errors occurred - detail them here
               alert("Transmission failed. (An error has occurred)");
           }
       });

With above code we can post data cross domain an everything is ok. But when i use this code:

使用上面的代码,我们可以发布跨域数据,一切正常。但是当我使用这段代码时:

$.post(' http://x.com/AnnouncementCategory/Save')

I get this error:

我收到此错误:

OPTIONS http://x.com/AnnouncementCategory/Save Request header field X-Requested-With is not allowed by Access-Control-Allow-Headers. jquery-1.9.1.js:8526 XMLHttpRequest cannot load http://x.com/AnnouncementCategory/Save. Request header field X-Requested-With is not allowed by Access-Control-Allow-Headers.

Access-Control-Allow-Headers不允许选项http://x.com/AnnouncementCategory/Save请求标题字段X-Requested-With。 jquery-1.9.1.js:8526 XMLHttpRequest无法加载http://x.com/AnnouncementCategory/Save。 Access-Control-Allow-Headers不允许请求标头字段X-Requested-With。

I see the jquery source code:

我看到了jquery源代码:

function ( url, data, callback, type ) {
        // shift arguments if data argument was omitted
        if ( jQuery.isFunction( data ) ) {
            type = type || callback;
            callback = data;
            data = undefined;
        }

        return jQuery.ajax({
            url: url,
            type: method,
            dataType: type,
            data: data,
            success: callback
        });
    }

Jquery also use ajax in post. **I know what is my error and just want to know:**What is the difference between $.ajax with type: post and jquery post?

Jquery也在帖子中使用ajax。 **我知道我的错误是什么,只是想知道:** $ .ajax与type:post和jquery post有什么区别?

2 个解决方案

#1


3  

jQuery's $.ajax method always sends the "x-requested-with" header for any cross domain requests, unlike the $.post. The error you are getting is because of the way external server is handling the external request. Please look here to get more info how the CORS (Cross Origin Resource Sharing – i.e. cross domain Ajax) is being handled. Also here you will find the similar problem and the solution.

与$ .post不同,jQuery的$ .ajax方法总是为任何跨域请求发送“x-requested-with”标头。您获得的错误是由于外部服务器处理外部请求的方式。请查看此处以获取有关如何处理CORS(跨源资源共享 - 即跨域Ajax)的更多信息。在这里你会发现类似的问题和解决方案。

#2


1  

The simple answer to the question you have asked is, a shorthand version of $.ajax, as described in the documentation:

您提出的问题的简单答案是,$ .ajax的简写版本,如文档中所述:

http://api.jquery.com/jQuery.post/

http://api.jquery.com/jQuery.post/

The docs do state that:

文档确实说明:

Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, or protocol.

由于浏览器安全限制,大多数“Ajax”请求都遵循相同的原始策略;请求无法成功从其他域,子域或协议中检索数据。

The question that you didn't ask, but perhaps is what you would really like to ask, is "why does cross-domain request work for me using $.ajax with a simple POST type, but not with $.post?". For that you would probably need to provide a bit more information.

您没有问的问题,但也许是您真正想问的问题,“为什么跨域请求对我来说使用$ .ajax使用简单的POST类型,但不能使用$ .post?”。为此,您可能需要提供更多信息。

#1


3  

jQuery's $.ajax method always sends the "x-requested-with" header for any cross domain requests, unlike the $.post. The error you are getting is because of the way external server is handling the external request. Please look here to get more info how the CORS (Cross Origin Resource Sharing – i.e. cross domain Ajax) is being handled. Also here you will find the similar problem and the solution.

与$ .post不同,jQuery的$ .ajax方法总是为任何跨域请求发送“x-requested-with”标头。您获得的错误是由于外部服务器处理外部请求的方式。请查看此处以获取有关如何处理CORS(跨源资源共享 - 即跨域Ajax)的更多信息。在这里你会发现类似的问题和解决方案。

#2


1  

The simple answer to the question you have asked is, a shorthand version of $.ajax, as described in the documentation:

您提出的问题的简单答案是,$ .ajax的简写版本,如文档中所述:

http://api.jquery.com/jQuery.post/

http://api.jquery.com/jQuery.post/

The docs do state that:

文档确实说明:

Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, or protocol.

由于浏览器安全限制,大多数“Ajax”请求都遵循相同的原始策略;请求无法成功从其他域,子域或协议中检索数据。

The question that you didn't ask, but perhaps is what you would really like to ask, is "why does cross-domain request work for me using $.ajax with a simple POST type, but not with $.post?". For that you would probably need to provide a bit more information.

您没有问的问题,但也许是您真正想问的问题,“为什么跨域请求对我来说使用$ .ajax使用简单的POST类型,但不能使用$ .post?”。为此,您可能需要提供更多信息。