PhoneGap - JSONP请求

时间:2016-04-29 14:08:53

标签: phonegap-build

经过大量的Google搜索试图找到解决这个问题的方法,我似乎已经有了,因为某些原因,大多数答案对我不起作用,所以我想我应该问一下那里的聪明人:)

我的问题是由于某些原因我的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构建它之后它不起作用:(

1 个答案:

答案 0 :(得分:0)

对于遇到同样问题的人来说,这就是我修复它的方法:

<强> HTML

我补充说:

typealias CMAccelerometerHandler = (CMAccelerometerData?, NSError?) -> Void

<强>的Config.xml

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