1. 程式人生 > >Cordova Ajax請求跨域問題整理

Cordova Ajax請求跨域問題整理

一、預設情況下Cordova在頁面中不能直接使用Ajax請求訪問外域的資料

異常1:

ripple.js:51 Refused to connect to 'http://localhost:59201/api/category/getlistbyparent?ProjectCode=lc_app_v1' because it violates the following Content Security Policy directive: "default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'". Note that 'connect-src' was not explicitly set, so 'default-src' is used as a fallback.


解決方案:

頁面級別配置,允許頁面的connect請求外域資料

設定Content-Security-Policy的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 *;">

異常2:

ripple.js:51 OPTIONS http://localhost:4400/ripple/xhr_proxy?tinyhippos_apikey=ABC&tinyhippos_rur…//localhost%3A59201/api/category/getlistbyparent%3FProjectCode%3Dlc_app_v1 net::ERR_CONNECTION_REFUSED


和異常3 Internet Server Error的問題

Error code is:xhr_proxy?tinyhippos_apikey=ABC&tinyhippos_rurl=http%3A//localhost%3A30673/ap‌?i/user/Get%3Fjson rippleapi.herokuapp.com Status Code:500 Internal Server Error I'am getting data from my localhost post adress:localhost:30673/api/user/Get'; It is working well in browser . And getting data from localhost:30673/api/user/Get. But in ripple it tries to get data from There: xhr_proxy?tinyhippos_apikey=ABC&tinyhippos_rurl=http%3A//localhost%3A30673/api/u‌?ser/Get%3Fjson rippleapi.herokuapp.com


解決方案:

Ripple配置

ipple設定右上角有一個Cross Domain Proxy,有三個選擇,Disabled、Local和Remote,通過字面意思就可以看出來分別對應禁用、本地和遠端訪問,如果是訪問本機的api的話,一般設定為Disabled就可以了。如果訪問遠端主機的api,一般要設定為Remote或Disabled。

更多: