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

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

这个问题部分指向我之前的问题:

但我找不到可行的解决方案。

问题如下:

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

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

我认为这是由 Angular http 方法引起的问题,但在之前的 post 我发现这不是真的。

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

非常感谢您的任何建议。

编辑:

我也试过这样设置cache:false:

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

但运气不好。

一个 are .

我刚刚 运行 遇到了同样的问题。这似乎与新版本的 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-inlineunsafe-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

然后重新构建您的应用,它应该会再次运行。

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

cordova plugin remove cordova-plugin-whitelist

然后重新安装

cordova plugin add cordova-plugin-whitelist

然后成功了

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

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

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

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

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

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

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