我尝试在 http angularJS 中使用 put 方法,但出现错误
错误:XMLHttpRequest 无法加载 [url]。预检响应具有无效的 HTTP 状态代码 404
我的代码是:
var newRentalUrl = serverURL;
var dataObj = JSON.stringify(JSONDATA);
$http.put(newRentalUrl, dataObj, {
headers: {
'Content-Type': 'application/json; charset=UTF-8'
},
}).success(function(responseData) {
try {
$ionicLoading.hide();
console.log(responseData);
//$state.go('payment');
} catch (err) {
console.log(err);
$ionicLoading.hide();
}
$ionicLoading.hide();
}).error(function(data, status, headers, config) {
$ionicLoading.hide();
console.log(data);
});
在服务器端,该方法作为选项被接收
2015-10-21T05:37:02.947517+00:00 heroku[router]: at=info method=OPTIONS path="/v1/home/xx" host=xxxxxx.herokuapp.com request_id=xxxxx-498d-4026-a39a-xxxxxxx fwd="xxx.xxxx.xxx.xxx" dyno=web.1 connect=0ms service=2ms status=404 bytes=481
最佳答案
将 Content-Type 更改为 'Content-Type': 'application/x-www-form-urlencoded;字符集=UTF-8'
如果是跨域资源请求,您需要在服务器中添加设置 CORS header ( Access-Control-Allow-Origin )。
希望它能起作用。
var newRentalUrl = serverURL;
var dataObj = JSON.stringify(JSONDATA);
$http.put(newRentalUrl, dataObj, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
},
}).success(function(responseData) {
try {
$ionicLoading.hide();
console.log(responseData);
//$state.go('payment');
} catch (err) {
console.log(err);
$ionicLoading.hide();
}
$ionicLoading.hide();
}).error(function(data, status, headers, config) {
$ionicLoading.hide();
console.log(data);
});
关于javascript - Http Put 方法在带有 header 的 AngularJS 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33251837/