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
你可以复制粘贴全部响应? –