我刚刚更新了 Angular + Ionic 的新版本,处理远程请求的方法停止工作并始终返回 404 响应。
请求如下:
Request Method:POST
Status Code:404 Not Found (from cache)
Request Headersview source
Accept:application/json, text/plain, */*
Content-Type:text/plain
Origin:file://
User-Agent:Mozilla/5.0 (Linux; Android 4.4.2; Lenovo Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Mobile Safari/537.36
Request Payloadview source
{,…}
处理远程请求的方法代码如下:
// set transfer credentials
$http({
method : 'POST',
url : $scope.remoteUrl,
data: {img_base64: "/9j/4AAQSkZ"},
headers: 'application/json',
timeout: 10000
// success response
}).success(function(data, status, headers, config) {
//SUCESSS
} else {
//PROCESSING ERROR
}
// error response
}).error(function(data, status, headers, config) {
// ERROR
});
我尝试使用这个主题来解决它:
AngularJs $http.post() does not send data
和
Angular + Ionic Post request getting 404 not found
但没有运气。
服务器端是这样处理请求的:
$inputJSON = file_get_contents('php://input');
$input= json_decode( $inputJSON, TRUE ); //convert JSON into array
如果我尝试使用 Postman 或 Curl 发送请求,一切似乎都正常。
ionic 信息:
Node Version: v0.12.2
Cordova CLI: 5.0.0
Ionic CLI Version: 1.3.22
Xcode version: Xcode 6.3.1 Build version 6D1002
ios-sim version: Not installed
ios-deploy version: Not installed
AngularJS 版本:
"version": "1.3.13",
请问如何解决?
非常感谢您的任何建议
最佳答案
嗯,我刚刚遇到了同样的问题: header 表明它已从缓存
中获取...但实际上,它似乎与新版本中的新安全策略有关 Cordova 。
我是这样解决的:
我安装了 Cordova's whitelist plugin :
cordova plugin add cordova-plugin-whitelist
然后,将您的内容策略作为元标记添加到您的 index.html
中(使用您自己的主机或“*”来接受所有请求):
<meta http-equiv="Content-Security-Policy" content="default-src 'self' yourhost.com ws://localhost:35729 data: gap: https://ssl.gstatic.com; style-src 'self' 'unsafe-inline'; media-src *;script-src 'self' localhost:35729 'unsafe-eval' 'unsafe-inline';">
default-src
用于一般请求; ws://localhost:35729
主机用于 ionic serve
中的实时重新加载。
script-src
用于安全脚本执行
unsafe-inline
和 unsafe-eval
是 Angular 正常工作所必需的。
data: gap: https://ssl.gstatic.com
仅在 iOS 上使用。
self
表示 index.html
文件的当前宿主。
You'll have to add your own in order for your requests to work. Don't forget to add the protocol and the port if they're non-standard
You can skip the meta tag if you don't want it, but you'll get a lot of warnings from the whitelist plugin.
有关如何配置的更多信息,请参阅 the plugin's readme .
然后重新构建您的应用,它应该会再次运行。
关于javascript - Ionic + Angular POST 请求返回状态 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30122890/