2015-11-22 138 views
4

當我在Android模擬器或Android設備上運行我的應用程序時,出現以下所有AJAX請求錯誤:Apache Cordova:加載資源失敗:服務器響應的狀態爲404(未找到)

Failed to load resource: the server responded with a status of 404 (Not Found) 

我嘗試了以下所有步驟來解決此問題,但它仍然存在。

  • 使用npm將白名單插件安裝到項目中。
  • 已添加<plugin name="cordova-plugin-whitelist" version="1" />至 config.xml。
  • 向平臺\ android \ AndroidManifest.xml添加了<uses-permission android:name="android.permission.INTERNET" />
  • 增加<meta http-equiv="Content-Security-Policy" content="default-src * 'unsafe-inline';"><meta http-equiv="X-XSS-Protection" content="0">到www/index.html文件的<head>
  • <access origin="*" />,<allow-navigation href="*" /><allow-intent href="*" />添加到config.xml文件中。

無論我還是得到相同的錯誤。有任何想法嗎?

項目編譯得很好。我在Windows 7上,使用科爾多瓦5.4.0,安卓5.1.1

回答

10

刪除應用程序的與整個Android部分:

cordova platform remove android 

,並重新加入:

cordova platform add android 

解決了這個問題,這仍然是一個謎給我。

也許從科爾多瓦的早期版本出現了一些問題,與當前的科爾多瓦版本不兼容。

1

我認爲問題與您的內容安全策略元標記 - 嘗試添加*到默認src打開Ajax請求到任何地方。您還可以添加專門用於Ajax請求的connect-src子句,並列出您希望能夠訪問的主機和協議。例如:

<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; connect-src http://myhost.mydomain.com"> 

將使Ajax請求到http://myhost.mydomain.com

我寫了一篇博客文章討論這個話題,你可能會發現有用的,它可以是read here

+0

我很抱歉,但我不小心從刪除的通配符*我的Content-Security-Policy標籤,事實上包含了它,我編輯了我的問題。所以這並不能解決問題。我試過你的內容安全策略元來限制請求只有一個域名,我仍然得到: '404(Not Found)' 但是,對於內容安全策略實際上不允許的其他域現在,我得到: '拒絕加載腳本,因爲它違反了以下內容安全策略...' 因此,可能是404錯誤是由Content-Security-Policy以外的其他內容引起的。如何調查更多? – Kaspi

+0

我會嘗試使用[Chrome遠程調試工具](https://developers.google.com/web/tools/chrome-devtools/debug/remote-debugging/remote-debugging?hl=en)並查找任何JavaScript控制檯錯誤,以及嘗試Ajax調用時網絡選項卡上發生的情況。這可能會讓問題更清楚。 –

-3

我試着刪除android平臺並重新添加它。這根本不適用於我。實際上,我注意到當構建到模擬器中時,其中一個構建步驟將刪除cordova-plugin-whitelist。

1

更新的Android

cordova platform update [email protected] 

我有問題,我連接到PHP頁面我的服務器上,我做了一個更新到Android

相關問題