2016-08-30 51 views
1

我想在角度的http調用中發送文檔cookie。 我已經使用withCredentials,但它只能發送瀏覽器級別的cookie。 我也使用跨域,但它不能正常工作。 下面是我正在使用的代碼。在ajax角度發送餅乾

$http({ 
    method: 'GET', 
    url: url, 
    xhrFields: { withCredentials: true }, 
    crossDomain: true,  
    header:{ 'SHOPSESSIONID' : sessionStorage.getItem("SHOPSESSIONID") } 
}).success(function(data){ 
    return data; 
}).error(function(data){ 
    return data; 
}); 
+0

是什麼 – Aravind

回答

0

使用$httpProvider在配置階段:

app.config(['$httpProvider', function ($httpProvider) { 
    $httpProvider.defaults.withCredentials = true; 
}]) 

,你可以放棄xhrFieldscrossDomain proporties

順便說一句,棱角分明的用途then代替success,並catch而不是error。 (也就是promise語法)

+0

添加行後,顯示「通配符'*'不能在'Access-Control-Allow-Origin'標題中使用,當憑證標誌爲真時」現在使用下面的代碼 (function ){'use strict'; angular.module('app').config(['$ httpProvider',function($ httpProvider){$ httpProvider.defaults.withCredentials = true; $ httpProvider.defaults.crossDomain = true;}] ).service('UtilityService',['$ log','$ q','$ http' ,函數($ log,$ q,$ http){this.getHTTPGET = function(url){return $ http({method:'GET',url:url})。then(function(data){return data;} ).catch(function(data){return data;});};}]);})(); –

+1

你需要在服務器端更改'Access-Control-Allow-Origin'頭,查看http://stackoverflow.com/questions/19743396/cors-cannot-use-wildcard-in-access-control-allow-origin - 當-憑證標誌-I – MoLow

0

Access-Control-Allow-Origin需要爲所謂的URL在PHP服務器端代碼波紋管設置,如工作形式,同樣的問題,「通配符‘*’....

//CROS/enable cookie saving on remote 
if(isset($_SERVER['HTTP_ORIGIN'])){ 
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}"); 
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept"); 
    header("Access-Control-Allow-Credentials: true"); 
    header("Access-Control-Allow-Methods: GET"); 
}