我试图通过axios在Vue中发出一个CORS get请求。到目前为止,一切都很好,但是如果我需要通过基本认证,我无法让它工作。
这是我的代码
getData: function() {
  let vm = this; // assign vue instance to vm
  axios.get('url', {
    withCredentials: true,
    auth: {
      username: 'name',
      password: 'pass'
    }
  }).then(function(response) {
    console.log(response)
  }).catch(function(error) {
    console.log(error)
  }) 
}即使我输入了凭据,以及正确的url,我总是收到401的回复。它看起来像这样。
HTTP/1.1 401 Authorization Required
Date: Wed, 01 Feb 2017 16:23:31 GMT
WWW-Authenticate: Basic realm="'Realm'"
Content-Length: 499
Keep-Alive: timeout=15
Connection: Keep-Alive
Content-Type: text/html; charset=iso-8859-1原始请求看起来是这样的
OPTIONS 'url' HTTP/1.1
Host: 'host'
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Access-Control-Request-Method: GET
Origin: http://localhost:9000
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.76
Safari/537.36
Access-Control-Request-Headers: authorization
Accept: */*
DNT: 1
Referer: http://localhost:9000/pages/blog_cc/blog_cc.html
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: de,en;q=0.8我遗漏了什么?有了邮差和完全相同的证件,一切都像魅力一样运作。即使是直接在Chrome。
我看到这个问题已经问过几次了,但其他线程上的所有答案对我来说都不是解决办法。例如,我不能从节点发出请求。
编辑:我也试着用香草JS发出请求,也没有结果。问题似乎在后端。不过,这是我的密码。
let xml = new XMLHttpRequest();
xml.open('GET', api, true);
xml.setRequestHeader('Authorization', 'Basic: ' + btoa(user + ':' + pass));
xml.send();发布于 2018-08-13 21:04:34
我有同样的经验,我的解决方法是使用以下设置来处理与我们设置的OAuth服务器的通信:
axios({
  headers: { "Content-Type": "application/x-www-form-urlencoded" },
  method: "post",
  url: REVOKE_URL,
  auth: {
    username: CLIENT_ID,
    password: CLIENT_SECRET
  },
  data: {
    datastuff: actualData
  }
});几天来,在使用axios.post的尝试都是徒劳的之后,这种方法就起了作用。
Axios版本: 0.18.0
发布于 2017-11-07 13:05:11
因此,我在寻找一个例子,如何做一个帖子,但没有找到一个好的。它起作用了,看起来就像:
const { MAILCHIMP_KEY } = process.env;
const url = "https://us{yourRegionNumber}.api.mailchimp.com/3.0/lists/{yourlistid}/members/";
const client = axios.create({
  auth: {
    username: "yourmailchimpusername",  //This could be your email
    password: MAILCHIMP_KEY
  },
  headers: {
    "Content-Type": "application/json"
  }
});
const mailChimpRes = await client.post(url, {
  email_address: `${email}`,
  status: "subscribed",
  merge_fields: {
    FNAME: `${firstName}`,
    LNAME: `${lastName}`
  }
});发布于 2017-02-01 16:45:04
如果是跨站点请求,可以尝试将withCredentials: true,选项传递给Axios请求。
https://stackoverflow.com/questions/41985042
复制相似问题