Standard XMLHttpRequest works, but not jQuery $ .ajax during POSTing to…

I want to make a POST to a REST service. When using the standard XMLHttpRequest-way it works, but when using jQuery $.ajax the HTTPRequest headers gets messed up, and the POST becomes OPTIONS.

I’m new to jQuery and HTTP, so I might have missed something obvious :)

This works:

function sendPostXMLHTTP() {
    var jData = { "Name": "Olle" };

    var client = new XMLHttpRequest();
    client.open("POST", "http://localhost:8383/DEMOService/TestPost");
    client.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
    client.send(JSON.stringify(jData));
}

This does not work:

function sendPostAjax() {
    var jData = { "Name": "Olle" };

    $.ajax({
        type: 'POST',
        url: 'http://localhost:8383/DEMOService/TestPost',
        contentType: "text/plain;charset=UTF-8",
        data: JSON.stringify(jData)
    });
}

HTTPRequest generated from from sendPostXMLHTTP() ( correct
):

POST http://localhost:8383/DEMOService/TestPost HTTP/1.1
Host: localhost:8383
Connection: keep-alive
Content-Length: 15
Origin: http://localhost:9990
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.79 Safari/535.11
Content-Type: text/plain;charset=UTF-8
Accept: */*
Referer: http://localhost:9990/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: sv-SE,sv;q=0.8,en-US;q=0.6,en;q=0.4
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

{"Name":"Olle"}

HTTPRequest generated from from sendPostAjax() incorrect
:

OPTIONS http://localhost:8383/DEMOService/TestPost HTTP/1.1
Host: localhost:8383
Connection: keep-alive
Access-Control-Request-Method: POST
Origin: http://localhost:9990
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.79 Safari/535.11
Access-Control-Request-Headers: Origin, X-Requested-With, Content-Type, Accept
Accept: */*
Referer: http://localhost:9990/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: sv-SE,sv;q=0.8,en-US;q=0.6,en;q=0.4
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

Why does the “jQuery $.ajax”-way turn the POST into a OPTIONS, and why does the request body dissapear?

// Patrik

jQuery assumes that you’re doing a Cross-Origin Resource Sharing
request. By W3C standard any CORS request must be preceded by empty OPTIONS request asking server to allow the subsequent request with payload.

Are you doing a request to the server other that your script is hosted on?

If yes, then jQuery’s request is correct and XMLHttpRequest’s is not – you should implement OPTIONS preflight request yourself.

If no then you should consider changing something in your configuration – for example, moving a script to the same website where your REST service resides.

OR, as suggested in comments, this might be a bug in jQuery or browser you’re using.

Hello, buddy!责编内容来自:Hello, buddy! (源链) | 更多关于

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 前端开发 » Standard XMLHttpRequest works, but not jQuery $ .ajax during POSTing to…

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录