Phonegap + jQuery Ajax Post不起作用 [英] Phonegap + jQuery Ajax Post not working
问题描述
我试图从我正在使用jQuery的phonegap应用中将数据作为POST数据发送到本地页面:
I am trying to send data to a local page as POST data, from a phonegap app that I am working on using jQuery:
$.ajax({
method: "POST",
url: "http://.../api_return.php",
data: { name: "John", location: "Boston" }
})
.done(function( msg ) {
alert( "Data Saved: " + msg );
});
在我的api_return.php
页面上,我有以下代码:
on my api_return.php
page I have the following code:
<?php
print_r($_REQUEST);
?>
奇怪的是,如果我创建了url: "http://.../api_return.php?test=true",
,那么我的php返回了test=true
,我无法获取它来返回POST
数据.
The strange thing is that if I make the url: "http://.../api_return.php?test=true",
then my php returns the test=true
, I cannot get it to return POST
data.
我已经尝试过:config.xml
中的
<access origin="*" />
AndroidManifest.xml中的<uses-permission android:name="android.permission.INTERNET" />
I have tried:<access origin="*" />
in config.xml
<uses-permission android:name="android.permission.INTERNET" />
in AndroidManifest.xml
推荐答案
我设法使它起作用,但是不幸的是,我不知道哪个更改最终使它起作用.这是我所做的更改的列表:
I managed to get it to work, but unfortunately I do not know which change ended up making this work. Here is a list of the changes I made:
1)在config.xml中添加<access origin="*" />
1) add <access origin="*" />
in config.xml
2)在AndroidManifest.xml中添加<uses-permission android:name="android.permission.INTERNET" />
2) add <uses-permission android:name="android.permission.INTERNET" />
in AndroidManifest.xml
3)在content=""
内为元标记添加connect-src *;
:<meta http-equiv="Content-Security-Policy" content="" />
3) add connect-src *;
inside of content=""
for the meta tag: <meta http-equiv="Content-Security-Policy" content="" />
4)将数据包装在JSON.stringify中.结束ajax通话:
4) Wrapped data in JSON.stringify. Ending ajax call:
$.ajax({
type: "POST",
url: "http://www.example.com/apicallback.php", // Example
data: JSON.stringify({ "name": "John", "location": "Boston" }),
success: function(response) {
console.log(response);
alert(JSON.stringify(response));
},
error: function(e) {
alert('Error: ' + e.message);
}
});
5)重定向到第二个URL(一个未使用.htaccess文件夹中的RewriteCond
的URL)
5) Redirected to a second URL (one that was not using RewriteCond
in the .htaccess folder)
这篇关于Phonegap + jQuery Ajax Post不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!