简体   繁体   中英

jQuery.ajax is not sending an XMLHTTPRequest header

Using 1.7.2, I'm submitting a form using jQuery.ajax():

jQuery.ajax({
    url: form.attr('action'),
    action: 'POST',
    dataType: 'json',
    data: {post_id: id},
    success: function(data) {
        console.log('hurrah!');
    },
    error: function() {
        console.log('whoops');
    }
});

I was expecting the .ajax() call to set the XMLHTTPRequest header, but it isn't:

Referer: http://0.0.0.0:5000/messages/news-feed
Origin: http://0.0.0.0:5000
Content-Length: 42
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.18.5 (KHTML, like Gecko) Version/5.2 Safari/535.18.5
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate

I've tried adding the headers setting to the call, but it's not making any difference; the header isn't being set: "X-Requested-With":"XMLHttpRequest"

I've also tried

beforeSend: function (request) {
    request.setRequestHeader("X-Requested-With", "XMLHttpRequest");
}

Try this

$.ajax({
url: form.attr('action'),
dataType:'json',
type:'POST',
success:function(data){
console.log(data);
},
error:function(jxhr){
console.log(jxhr.responseText);
}

}); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM