2016-04-29 124 views
0

經過大量谷歌搜索試圖找到這個問題的解決方案,我似乎有,由於某些原因,大多數答案不適合我,所以我想我應該問這裏的聰明人:)PhoneGap - JSONP請求

我的問題是由於某種原因,我的JSONP張貼到服務器不工作:

HTML

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8" /> 
    <meta name="format-detection" content="telephone=no" /> 
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" /> 
    <link rel="stylesheet" type="text/css" href="css/style.css" /> 
</head> 

<body> 

<p> 
    <button id="sendPost">Send POST</button> 
</p> 

<div id="results"></div> 
<div id="response"></div> 

    <script type="text/javascript" src="cordova.js"></script> 
    <script type="text/javascript" src="js/jquery.min.js"></script> 
    <script type="text/javascript" src="js/app.js"></script> 
</body> 
</html> 

JS

window.onerror = function(err, fileName, lineNumber) { 
alert(fileName, 'Line:', lineNumber, 'Error:', e.message); 
}; 

document.addEventListener("deviceready", init, false); 

function init() { 
document.querySelector("#sendPost").addEventListener("touchend", sendPost, false); 
} 

function sendPost() { 

var myData = { 
    "name": "Mike" 
}; 

jQuery.support.corps = true; 

jQuery.ajax({ 
    url: 'http://www.example.com/service.php', 
    dataType: 'jsonp', 
    data: myData, 
    success: function(data) { 
     console.log("Success: ", data); 
    }, 
    error: function(e) { 
     $('#response').html('There was an error loading the data.' + JSON.stringify(e)); 
     $('#response').append('Parse error:' + e.parsererror); 

    } 
}); 
} 

的Config.xml

<?xml version="1.0" encoding="UTF-8" ?> 
<widget xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0" id="com.phonegap.example" versionCode="10" version="1.0.0"> 

<name>My Ajax App</name> 

<description> 
    Do an ajax request 
</description> 

<access origin="*" /> 
<allow-intent href="*" /> 
<allow-navigation href="*" /> 

<platform name="android" /> 
<preference name="EnableViewportScale" value="true" /> 

</widget> 

這是我爲我建立的應用程序是什麼,它只是需要張貼到服務,它工作在本地但我建立後移動它phongap建立它不工作:(

+0

這裏沒有聰明的人。 - Phonegap不是[網絡服務器或網頁瀏覽器](https://github.com/jessemonroy650/top-phonegap-mistakes/blob/master/new-to-Phonegap.md#005)。這個FAQ也應該有所幫助。首先閱讀**粗體句子。 [Cordova/Phonegap新手開發人員犯的錯誤](https://github.com/jessemonroy650/top-phonegap-mistakes/blob/master/new-to-Phonegap.md) – JesseMonroy650

回答

0

對於任何人誰有同樣的問題,這是我的固定它:

HTML

我說:

<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'" /> 

的Config.xml

<access origin="*" /> 
<allow-intent href="*" /> 
<allow-navigation href="*" /> 

<gap:plugin name="cordova-plugin-whitelist" source="npm" />