在angularjs中为get请求设置标头

时间:2016-04-20 06:23:25

标签: angularjs

我正在尝试使用get方法发送标头,但每次都不会发送标头。 这是我的app.js

       var config = { headers:{
                      'Accept': 'application/json;odata=verbose',
                      "authId" : "236643d1-b4c8-4211-97ba-1f24e93b8568"
                             }
                    };

    $http.get("http://52.36.44.187:8080/maistylz-webservice/me", config).then(function(data){
   console.log(data);
  }, function (error) {
console.log(angular.toJson(error));});

这是请求标头

Accept:*/*/
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:accept, authid
Access-Control-Request-Method:GET
Cache-Control:no-cache
Connection:keep-alive
Host:52.36.44.187:8080
Origin:http://localhost
Pragma:no-cache
Referer:http://localhost/swagger/login.php
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36



Request URL:http://52.36.44.187:8080/maistylz-webservice/me
    Request Method:OPTIONS
    Status Code:403 Forbidden
    Remote Address:52.36.44.187:8080

响应标头

Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:origin, content-type, accept, authId
Access-Control-Allow-Methods:GET,POST,DELETE,PUT,HEAD
Access-Control-Allow-Origin:http://localhost
Access-Control-Max-Age:1800
Content-Length:17
Content-Type:application/vnd.sun.wadl+xml
Date:Wed, 20 Apr 2016 06:13:06 GMT
Server:Apache-Coyote/1.1

显示403状态。代码有什么问题? 我必须在标题中传递authId。 这是必要的。

这是控制台日志

catcher.js:197 OPTIONS http://52.36.44.187:8080/maistylz-webservice/me XMLHttpRequest.send @ catcher.js:197(anonymous function) @ angular.js:11756m @ angular.js:11517g @ angular.js:11227(anonymous function) @ angular.js:15961n.$eval @ angular.js:17229n.$digest @ angular.js:17045n.$apply @ angular.js:17337(anonymous function) @ angular.js:1749invoke @ angular.js:4665c @ angular.js:1747yc @ angular.js:1767ee @ angular.js:1652(anonymous function) @ angular.js:30863b @ angular.js:3166Qf @ angular.js:3456Pf.d @ angular.js:3444
login.php#0:1 XMLHttpRequest cannot load http://52.36.44.187:8080/maistylz-webservice/me. Response for preflight has invalid HTTP status code 403

0 个答案:

没有答案