1
我在調用Web服務時遇到了AngularJS中的CORS請求問題,但是可以使用jQuery調用相同的服務。如何在AngularJS中處理CORS請求
注意:從服務器端我們正在接收標頭「訪問控制 - 允許來源:*」並且這些服務在jQuery應用程序中運行良好。
在這裏,我發佈我的AngularJS代碼以及jQuery代碼。
AngularJS:
$http({
method: 'POST',
url: $rootScope.host + "UserLogin",
//headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
data: {
"uname": $scope.uname,
"password": $scope.password
},
}).then(function (success) {
$scope.loginDetails = success;
console.log($scope.loginDetails);
}),function (error){
console.log(error);
});
如果我傳似headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
能ping服務的頭,但我的要求是不是以JSON格式去。
如果我改變頭'Content-Type': 'application/json'
,越來越
的XMLHttpRequest無法加載https://XXXX.XXXX.in/XXXXAPI/UserLogin。
響應預檢請求未通過訪問控制檢查:
否「訪問控制允許來源」標題存在於所請求的資源。因此不允許訪問原產地'http://170.11.0.61'。
我不知道這個錯誤的原因是什麼。
$.ajax({
url: BASE_URL + "UserLogin",
type: "POST",
xhrFields: {withCredentials: true},
data: {
"uname": uname,
"password": password
},
cache: false,
success: function (result, textStatus, request) {
console.log(result);
},
error: function (e) {
console.log("Error in login service call:"+JSON.stringify(e));
}
});
這個jQuery發送我的請求在json格式。
如果我改變了我的頭,我的服務電話沒有ping服務器,並顯示「的XMLHttpRequest無法加載https://XXXX.XXXX.in/XXXXXAPI/UserLogin。響應預檢請求未通過訪問控制檢查:請求的資源上沒有「訪問控制 - 允許 - 來源」標頭,因此不允許Origin'http://172.16.0.61'訪問。 – srihari