jquery - 无法让 jQuery post 请求在phonegap 中工作

标签 jquery cordova

我正在尝试使用 jQuery 和 Ajax 将一些表单数据发送到phonegap 中的远程服务器。我在下面粘贴的代码在我的本地计算机上的 Firefox 中运行良好,但当我在手机(Android)上启动它时它将无法运行。我是否需要修改或添加一些内容到 jquery 代码中才能使其在phonegap 中工作?

我已在 Android list 中添加了 INTERNET 权限。

<!DOCTYPE HTML>
<html>
    <head>
        <title>Phonegap ajax test</title>

        <meta name="viewport" content="width=240, height=320, user-scalable=yes, initial-scale=2.5, maximum-scale=5.0, minimum-scale=1.0" />
        <script type="text/javascript" charset="utf-8" src="phonegap-1.1.0.js"></script>
        <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
        <script type="text/javascript" charset="utf-8" src="js/jquery.mobile-1.0rc2.js"></script>

        <link rel="stylesheet" type="text/css" href="css/layout.css" />
        <link rel="stylesheet" type="text/css" href="js/jquery.mobile-1.0rc2.css" />

        <script type="text/javascript">    
            $(document).ready(function() {
                $('#infoForm').submit(function() {
                    var postTo = 'http://www.mysite.com/process.php';

                    $.post(postTo,$('#infoForm').serialize(), function(data) {
                        alert(data);       
                    });
                    return false;
                }); 

            });
        </script>
    </head>

    <body>      
        <div data-role="content">   
            <form method="post" id="infoForm">
                <input type="text" name="first_name" id="first_name" value="" placeholder="First Name"  />
                <input type="text" name="last_name" id="last_name" value="" placeholder="Last Name"  />   
                <input type="text" name="email" id="email" value="" placeholder="Email"  />
                <button type="submit">Submit</button> 
            </form>
        </div><!-- /content -->
    </body>
</html>

谢谢!

最佳答案

确保您在应用中拥有正确的权限。

添加

<uses-permission android:name="android.permission.INTERNET" />

AndroidManifest.xml并添加

<access origin="*"/>

/res/xml/phonegap.xml

关于jquery - 无法让 jQuery post 请求在phonegap 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8230321/

相关文章:

jquery - 我如何设置我的 json 文件正在打印到屏幕上的文本我想隐藏文本

jquery - 选择没有文本兄弟的内联元素

javascript - phonegap `volumedownbutton` 事件未触发

Cordova Background Fetcher 插件不适用于 iOS

jquery - 使 AJAX 请求转到正确的 URL

javascript - 如何在 JavaScript 中使用查询字符串

javascript - 为值 =""中的每个单词创建 <div>

android - 当设备的(android)键盘处于 Activity 状态时,jquery 移动弹出窗口小部件不会移动

node.js - 从 npm 下载 Phonegap 时出错

android - HTML5 地理定位不适用于 Android 上的仅 GPS(定位服务模式)