开发者

jQuery: get JSON via ajax, but with POST instead of GET

开发者 https://www.devze.com 2023-04-07 14:06 出处:网络
I\'m using jQuery\'s $.ajax to make a request to a third-party server, using JSONP.I specify the method as POST, but it uses GET anyway:

I'm using jQuery's $.ajax to make a request to a third-party server, using JSONP. I specify the method as POST, but it uses GET anyway:

    $.ajax({
        type: "POST",
        dataType: "json",
        url: other_server + "/run?callback=?",
        data: {
            code: $(code).val()
        },
        success: function(obj) {
            var res = obj.results;
            $(results).val(res);
        }
    });

Looking in th开发者_如何学编程e jQuery source, I see these two lines that seem to force all cross-domain requests to GET, but I don't understand why it needs to be so:

if ( s.crossDomain ) {
    s.type = "GET";

Is it possible to do this with a POST instead of a GET? Why does jQuery force the use of GET?


JSON-P works by inserting a <script> element into the document, hence it can only make GET requests.

If you want to make a POST request to a remote server then you need to look at XHR instead and set up CORS permissions. Note that this has limited browser support.

Alternatively, keep your requests to the same origin (and have your server make the request to the remote server).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号