Javascript Ionic + Angular - 如何在 POST 请求后避免“404 Not Found (from cache)”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30161952/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:45:04  来源:igfitidea点击:

Ionic + Angular - How to avoid the "404 Not Found (from cache)" after POST request?

javascriptphpangularjsposthttp-status-code-404

提问by redrom

this question is partly pointing to my previous question : Ionic + Angular POST request return state 404

这个问题部分指向我之前的问题: Ionic + Angular POST request return state 404

But i wasn't able to find working solution.

但我无法找到有效的解决方案。

Problem is following:

问题如下:

if I do request from the mobile app running on the device i always get response 404:

如果我确实从设备上运行的移动应用程序发出请求,我总是会收到响应 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
{,…}

But if i tried the same request on the desktop using Postman plugin in the Chrome browser i always get response 200 with data what i need.

但是如果我在 Chrome 浏览器中使用 Postman 插件在桌面上尝试相同的请求,我总是得到响应 200 和我需要的数据。

I thought that it was issue caused by Angular http method but in previous post i was noticed that is not true.

我认为这是由 Angular http 方法引起的问题,但在之前的帖子中我注意到这不是真的。

How can i avoid this please? Should i set some headers in HTML of the app, during the request or on the server side (PHP)?

我怎样才能避免这种情况?我应该在应用程序的 HTML 中、在请求期间还是在服务器端 (PHP) 中设置一些标头?

Many thanks for any advice.

非常感谢您的任何建议。

EDIT:

编辑:

I tried also set cache:false by this way:

我也尝试通过这种方式设置缓存:假:

$http({
                method : 'POST',
                url : $scope.remoteUrl,
                data: postData,
                headers: headersData,
                cache: false,
                timeout: 10000
                // success response
            })

But without luck.

但没有运气。

回答by Tiesselune

A lot of peopleare experiencing that.

一个很多人遇到

I just ran into the same problem. It seems it has to do with a new security policy in new versions of Cordova.

我刚刚遇到了同样的问题。这似乎与新版本 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.htmlas 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-srcis used for general requests; the ws://localhost:35729host is used for live-reload in ionic serve.

default-src用于一般请求;该ws://localhost:35729主机被用于活重装ionic serve

script-srcis used for secure script execution

script-src用于安全脚本执行

unsafe-inlineand unsafe-evalare required in order for angular to work properly.

unsafe-inline并且unsafe-eval是 angular 正常工作所必需的。

data: gap: https://ssl.gstatic.comis only used on iOS.

data: gap: https://ssl.gstatic.com仅在 iOS 上使用。

selfmeans the current host of the index.htmlfile.

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 Bruno Samard?i?

Had a same problem, nothing helped, then i just removed the whitelist plugin:

有同样的问题,没有任何帮助,然后我删除了白名单插件:

cordova plugin remove cordova-plugin-whitelist

then renstalled it

然后租了它

cordova plugin add cordova-plugin-whitelist

then it worked

然后它起作用了

回答by Enayat

I had the same problem and I could solve it by adding the following plugin:

我遇到了同样的问题,我可以通过添加以下插件来解决它:

ionic plugin add https://github.com/apache/cordova-plugin-whitelist.git

回答by Ewertom Moraes

After many hours, I found one solution that worked: run cordova plugin add cordova-plugin-whitelist

几个小时后,我找到了一个有效的解决方案:运行 cordova plugin add cordova-plugin-whitelist

In my case, I had already configured config.xml and index.html on origin, as mentioned here and elsewhere.

就我而言,我已经在原点配置了 config.xml 和 index.html,如此处和其他地方所述。

回答by Suhas s

I had a similar issue with Firebase sign on my android device but I could login on the browser.

我的 android 设备上的 Firebase 登录也有类似的问题,但我可以在浏览器上登录。

cordova plugin remove cordova-plugin-whitelist
cordova plugin add cordova-plugin-whitelist

Used the above lines to resolve the issue.

使用以上几行来解决问题。