jquery中没有xhr标头的普通http请求

Normal http request in jquery without xhr header

本文关键字:http 请求 xhr jquery      更新时间:2023-09-26

我想用这些头(或类似的东西)发送一个类似ajax的请求:

GET example.com/ajaxapi/article/1 HTTP/1.1
Host: localhost
Accept: application/hal+json
Cache-Control: no-cache
重要的一点是这样做没有X-Requested-With: XMLHttpRequest头。

. ajax()美元$ . get(),…

你可以使用beforeend函数修改header,该函数是ajax方法的settings对象的属性。

从文档中,beforeend的描述:

一个预请求回调函数,可以用来修改jQuery 1.4中的jqXHR。x, XMLHTTPRequest)对象。使用它来设置自定义头等。jqXHR和settings对象作为参数传递。这是一个Ajax事件。在beforeend函数中返回false将取消请求。从jQuery 1.5开始,无论请求的类型如何,都将调用beforeend选项。

的例子:

$.ajax('/yoururl', {
    beforeSend: function(jqXHR, settings) {
        // stuff goes here
    }
});

回答我自己的问题,如果它能帮助别人。

$.ajax({
    url:   url,
    crossDomain: true
});

crossDomain选项做到了这一点,并删除了XMLHttpRequest头。