Phonegap ajax表格不起作用

时间:2015-06-09 13:43:41

标签: javascript html ajax cordova

我的index.html

中有以下html / javascript
<html>
<head>
    <meta charset="utf-8" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="msapplication-tap-highlight" content="no" />
    <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
    <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/index.css" />
    <title>Hello World</title>
    <script type="text/javascript">
        function PostData() {
            // 1. Create XHR instance - Start
            var xhr;
            if (window.XMLHttpRequest) {
            xhr = new XMLHttpRequest();
            }
            else if (window.ActiveXObject) {
            xhr = new ActiveXObject("Msxml2.XMLHTTP");
            }
            else {
            throw new Error("Ajax is not supported by this browser");
            }
            // 1. Create XHR instance - End

            // 2. Define what to do when XHR feed you the response from the server - Start
            xhr.onreadystatechange = function () {
              if (xhr.readyState === 4) {
                if (xhr.status == 200 && xhr.status < 300) {
                document.getElementById('div1').innerHTML = xhr.responseText;
                }
            }
            }
            // 2. Define what to do when XHR feed you the response from the server - Start

            var userid = document.getElementById("userid").value;

            // 3. Specify your action, location and Send to the server - Start
            xhr.open('POST', 'localhost:3000/ajax');
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xhr.send("userid=" + userid);
            // 3. Specify your action, location and Send to the server - End
        };
    </script>
</head>
<body>


    <div class="app">

        <h1>Ajax Testarea</h1>

            <form>
                  <label for="userid">User ID :</label>
                  <input type="text" name ="userid" id="userid" />
                  <div id="div1"></div>
                  <input type="button" value ="Check" onclick="PostData()" />
            </form>
    </div>



    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript" src="js/index.js"></script>
    <script type="text/javascript">
        app.initialize();
    </script>
</body>

在我的Mac上工作得非常好但是当我尝试使用phonegap时,我无法点击按钮发送表单。因此它不会启动PostData()函数。

我也不确定将Javascript直接放在html中是否是一种好习惯,或者将它放在index.js中更好?我已经读到了把它放进去的内容:

onDeviceReady: function() {
        app.receivedEvent('deviceready');
   // ---My Code--- 
},

如果有人能指出我做错了什么,我会很高兴的!

1 个答案:

答案 0 :(得分:0)

我在使用Phonegap for iOS中的onClick和Jquery的Click功能时遇到了问题。我不得不使用带有touchstart的jQuery .On()函数

$('#buttonID').on({ 'touchstart' : function(){ /* Do stuff... */ } });

看起来你不是在使用jQuery,所以可以试试onTouch="function"onTouchStart="function"而不是onClick。