使用Postman进行GET方法Service调用时,我得到了一个成功的响应,但是当从Angular进行相同的调用时,我得到405错误。
var token = "sdfasdfasdfasdfaerqwelruaoeuapiou"
this.contentHeader = new Headers({"Content-Type":"application/json","Authorization":"Bearer"+token});
this.http = http;
this.url=IES.serviceUrl+'page/'+this.pageName;
this.http.get(this.url,{ headers: this.contentHeader }).map(res => res.json())
.subscribe(data => {
this.pageData = data;
this.pageControls = this.pageData.controls;
} , error => {
console.log('failed');
});
此代码在IE浏览器中运行时发送所需的“授权”标题,但无法在FF / chrome中发送标题
FF / Chrome浏览器的小提琴捕获是:
Request-Headers
...................
OPTIONS localurl HTTP/1.1
Host: localhost:21326
Connection: keep-alive
Access-Control-Request-Method: GET
Origin: http://localhost:8100
User-Agent: Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Mobile Safari/537.36
Access-Control-Request-Headers: authorization, content-type
Accept: */*
Referer: http://localhost:8100/
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: en-US,en;q=0.8
response(during angular call)
............
HTTP/1.1 405 Method Not Allowed
Cache-Control: no-cache
Pragma: no-cache
Allow: GET
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?RDpc2FyZA==?=
X-Powered-By: ASP.NET
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET,PUT,POST,DELETE,OPTIONS
Date: Thu, 23 Feb 2017 04:12:19 GMT
Content-Length: 76
{"Message":"The requested resource does not support http method 'OPTIONS'."}
firefox或IE浏览器中的Rest Client与
类似Request
.....................
GET localurl HTTP/1.1
Host: localhost:21326
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0
Accept: */*
Accept-Language: null
Accept-Encoding: gzip, deflate
Content-Type: application/json
Authorization: Bearer 9myBifb-HJG9tVpyopK495BA....String Truncated
Connection: keep-alive
Response
......................
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?RDpc2FyZA==?=
X-Powered-By: ASP.NET
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET,PUT,POST,DELETE,OPTIONS
Date: Thu, 23 Feb 2017 04:14:09 GMT
Content-Length: 221
{"PageId":1,"PageName":"dashboard","controls":["barchart","pieChart","scatterChart"],"lists":{},"actions":{}}
注意本
中的授权标题