IM需要複製這個jQuery Ajax功能:AngularJS發送POST數據如jQuery
$.ajax({
type: 'POST',
url: 'https://api.url.gift/api',
data: JSON.stringify({ "user_id": "100006" }),
success: function(result) {
console.log("asd");
console.log(result);
},
error: function(xhr, ajaxOptions, thrownError) {
console.log(xhr.status);
console.log(thrownError);
},
});
工作正常,但是當我這樣做是AngularJS:
VAR MYDATA的= JSON.stringify({」 user_id「:」100006「});
$http({
url: 'https://api.ecosquared.gift/things/cards/received_byme',
method: 'POST',
data: myData,
headers: { 'Content-Type': 'application/json; charset=UTF-8'},
}).success(function(data, status, headers, config){
console.log(data);
}).error(function(data, status, headers, config) {
console.log(data);
});
我得到:
XMLHttpRequest cannot load https://api.url.gift/api. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://url.gift' is therefore not allowed access. The response had HTTP status code 405.
一個奇怪的事情,如果我不發送數據參數,請求進入低谷和錯誤不要加薪,但當然,API不返回我期望的數據。
有人能指出我正確的方向嗎?
簡單的說,這是一個CORS請求,服務器通過添加上述標題專門爲您啓用。如果您無權訪問服務器代碼,請查看http://en.wikipedia.org/wiki/JSONP –
服務器標頭已到位,發佈請求返回:Access-Control-Allow-Origin:*。正如我所提到的,同樣的Jquery ajax請求工作正常,但角度doesen't – mdv
你可以複製粘貼全部響應? –