首页 > 一个post产生了两个请求

一个post产生了两个请求

我这里有一个跨域post请求,

var AjaxPost = (url, data, action ) => {

    let xhr = new XMLHttpRequest();
    xhr.open("POST", url, true);
    xhr.withCredentials = true
    xhr.setRequestHeader("Content-type", "application/json");
    xhr.onreadystatechange = () =>{
        if (xhr.readyState == 4 && xhr.status == 200) {
            //action是传入的callback
            action(JSON.parse(xhr.responseText))
        }
    }
    xhr.send(JSON.stringify(data))
}

export default AjaxPost

服务器用nginx做反向代理,配置如下

proxy_set_header Access-Control-Allow-Origin http://192.168.1.106:8080;
  add_header Access-Control-Allow-Headers X-Requested-With;
  add_header Access-Control-Allow-Methods GET,POST,OPTIONS;

但是post方式发送一个json数组到服务器却发现同样的路径不管在nginx的log和被代理的node服务器里看都是收到两个请求,一个是OPTIONS方法,一个是正常的POST方法,不知道是什么原因


建议你看看这篇文章,相信你会对跨域的理解更进一步,http://.com/a/1190000000709909


前两天我也碰到了这样的事情。
问题在于浏览器的跨域

MDN:HTTP access control

这个资源还是比较权威的。推荐阅读

【热门文章】
【热门文章】