javascript Ionic + Angular POST 请求返回状态 404
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30122890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ionic + Angular POST request return state 404
提问by redrom
I just updated on the new version on the Angular + Ionic and method for processing remote request stopped working and returns always 404 response.
我刚刚更新了 Angular + Ionic 的新版本,并且处理远程请求的方法停止工作并始终返回 404 响应。
Request is following:
请求如下:
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
{,…}
Code of the method which is processing remote request is following:
处理远程请求的方法代码如下:
// 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
});
I tried to solve it using this topic:
我试图用这个话题来解决它:
AngularJs $http.post() does not send data
and
和
Angular + Ionic Post request getting 404 not found
Angular + Ionic Post 请求找不到 404
But without luck.
但没有运气。
Server side is processing request by this way:
服务器端通过这种方式处理请求:
$inputJSON = file_get_contents('php://input');
$input= json_decode( $inputJSON, TRUE ); //convert JSON into array
If i'm trying to send request using Postman or Curl everything seems to be working.
如果我尝试使用 Postman 或 Curl 发送请求,一切似乎都在工作。
Ionic info:
离子信息:
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:
AngularJS 版本:
"version": "1.3.13",
How can i solve it please?
请问我该如何解决?
Many thanks for any advice
非常感谢您的任何建议
回答by Tiesselune
Hum, I just ran into the same problem: the header suggests it has been fetched from cache
... But actually, it seems it has to do with a new security policy in new versions of Cordova.
嗯,我刚刚遇到了同样的问题:标题表明它已被提取from cache
......但实际上,这似乎与新版本 Cordova 中的新安全策略有关。
Here's how I solved it:
这是我解决它的方法:
I installed Cordova's whitelist plugin:
我安装了Cordova 的白名单插件:
cordova plugin add cordova-plugin-whitelist
Then, add your content policy in your index.html
as a meta tag (using your own host or '*' for accepting all requests) :
然后,在您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
is used for general requests; the ws://localhost:35729
host is used for live-reload in ionic serve
.
default-src
用于一般请求;该ws://localhost:35729
主机被用于活重装ionic serve
。
script-src
is used for secure script execution
script-src
用于安全脚本执行
unsafe-inline
and unsafe-eval
are required in order for angular to work properly.
unsafe-inline
并且unsafe-eval
是 angular 正常工作所必需的。
data: gap: https://ssl.gstatic.com
is only used on iOS.
data: gap: https://ssl.gstatic.com
仅在 iOS 上使用。
self
means the current host of the index.html
file.
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.
您必须添加自己的请求才能使您的请求生效。如果它们是非标准的,请不要忘记添加协议和端口
如果您不想要元标记,您可以跳过它,但是您会从白名单插件中收到很多警告。
More info on how to configure this in the plugin's readme.
有关如何在插件的自述文件中进行配置的更多信息。
Then rebuild your app, and it should work again.
然后重建您的应用程序,它应该可以再次运行。
回答by Vishnu Kumar Soni
I also had this problem and searched a lot then finally... i removed the whitelist plugin:
我也有这个问题并搜索了很多然后最后......我删除了白名单插件:
cordova plugin remove cordova-plugin-whitelist
then renstalled it
然后租了它
cordova plugin add cordova-plugin-whitelist
It helped me and hope it solve your problem
它帮助了我,希望它能解决你的问题
回答by Wouter van Vegchel
I struggled for quite some time with this, but the trick was to install the plugin, configure the CSP in index.html and then remove the plugin an adding it again.
我为此苦苦挣扎了一段时间,但诀窍是安装插件,在 index.html 中配置 CSP,然后删除插件并再次添加它。
回答by Nopik
The (from cache)
in response status suggests that your browser cached bad reply and is playing it back to you. Try clearing cache or use any cache busting techniques.
该(from cache)
响应状态表明您浏览器的缓存不好答复,并播放回给你。尝试清除缓存或使用任何缓存破坏技术。