[英]Getting 400 bad request when trying to access an API using Angular, but it works perfectly fine when using PostMan
I have already tested this API and have made a GET request on the API using PostMan and got the response. 我已经测试过此API,并已使用PostMan对API进行了GET请求并获得了响应。 But when trying to implement it on my Angular 6 application I get a 401 unauthorized error. 但是,当尝试在我的Angular 6应用程序上实现它时,我收到401未经授权的错误。 This is my angular code. 这是我的角度代码。
this.http.get('https://api.channeladvisor.com/v1/orders',{headers:{'Authorization':'Bearer *token*','Content-Type':'application/x-www-form-urlencoded','cache-control':'no-cache'}})
.subscribe((response)=>{
this.channel=response;
console.log(this.channel)
})
}
I have the token and have not displayed it in the code for security reasons. 我有令牌,并且出于安全原因没有在代码中显示它。 I have a feeling it has something with the headers syntax but i'm not so sure. 我觉得它的标头语法有些问题,但我不太确定。
May you can try add them to your headers: 可以尝试将它们添加到标题中:
'authorization': 'bearer ' + token,
'Authenticationtoken': 'bearer ' + token,
'X-Requested-With' : 'XMLHttpRequest'
My API used CakePHP3, same issue happen long ago, after added above 3 parameters everything ok :) 我的API使用的是CakePHP3,同一问题很早以前就发生了,在添加上述3个参数之后,一切正常:)
And more, please track your request by developer tools, and compare the difference between sucess and failed. 另外,请通过开发人员工具跟踪您的请求,并比较成功与失败之间的区别。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.