jQuery.ajax没有发送XMLHTTPRequest标头

时间:2012-04-27 11:02:29

标签: jquery ajax xmlhttprequest

使用1.7.2,我使用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');
    }
});

我期待.ajax()调用设置XMLHTTPRequest标头,但它不是:

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

我尝试将headers设置添加到通话中,但它没有任何区别;标头未设置:"X-Requested-With":"XMLHttpRequest"

我也试过

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

1 个答案:

答案 0 :(得分:1)

试试这个

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

});