weixin_33701294 2017-12-26 05:01 采纳率: 0%
浏览 89

使用Axios的相同请求

I was consuming an api using XHR, but the same request does not work using axios.

With XHR:

var data = JSON.stringify(false);
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.open("POST", "api/url");
xhr.send(data);
xhr.addEventListener("readystatechange", function () {
  if (this.readyState === this.DONE) {
    console.log(this.responseText);
  }
});

With Axios:

axios({
  method: 'post',
  url: 'api/url',
  data: JSON.stringify(false),
  withCredentials: true
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

Error is

"Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource."

Response Headers on XHR

  • HTTP/1.1 200 OK
  • Accept-Ranges: bytes
  • Accept-Ranges: bytes
  • Access-Control-Allow-Credentials: true
  • Access-Control-Allow-Origin:http://localhost:8000
  • Age: 0
  • Cache-Control: no-cache, no-store, max-age=0, must-revalidate
  • Content-Type: application/json;charset=UTF-8
  • Date: Tue, 26 Dec 2017 13:50:01 GMT
  • Expires: 0
  • Pragma: no-cache
  • Vary: Origin

Response Headers on Axios:

  • HTTP/1.1 404 Not Found
  • Accept-Ranges: bytes
  • Age: 0
  • Content-Type: application/json; charset=utf-8
  • Date: Tue, 26 Dec 2017 14:01:26 GMT
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 树莓派与pix飞控通信
    • ¥15 自动转发微信群信息到另外一个微信群
    • ¥15 outlook无法配置成功
    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题