简体   繁体   English

尝试使用Angular访问API时遇到400错误的请求,但是使用PostMan时,它的工作效果很好

[英]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.

相关问题 Angular2:发布到 API 时不断收到 400 错误请求错误,但使用 RestClient 时我得到 200 Ok - Angular2: Keep getting a 400 bad request error when posting to API but when using a RestClient I get a 200 Ok Angular:我不断收到错误 400:使用 httpPost 时出现错误请求“发生一个或多个验证错误” - Angular: I keep on getting error 400: bad request "One or more validation error occured" when using httpPost 角度-使用PUT时出现400错误(错误请求) - Angular - 400 Error (Bad Request) when using PUT Angular 13 - 使用 PUT 时出现 400 错误(错误请求) - Angular 13 - 400 Error (Bad Request) when using PUT 在Angular 4中使用发布请求时出现400错误 - Getting 400 error when using post request in Angular 4 调用API url获取Auth令牌时的Angular 6:400错误请求 - Angular 6 : 400 bad request when calling API url for getting Auth token 安装Angular CLI时出现错误400错误请求 - Getting error 400 Bad Request when installing Angular CLI 尝试使用spring作为后端以角度2上传文件时出现错误请求 - Bad request when trying to upload file in angular 2 using spring as backend 获取 angular 8 发布 http 请求的问题,Postman 工作正常 - Problems with getting angular 8 to post http request, Postman works fine Angular CORS 预检请求导致 400 Bad Request,但 Postman 请求工作正常 - Angular CORS preflight request causes 400 Bad Request, but Postman request works OK
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM