无法使用PhoneGap在Windows Phone 7中调用Ajax方法

时间:2013-06-26 05:11:06

标签: jquery ajax cordova windows-phone-7 phonegap

我使用HTML5 / Javascript / JqueryAjax创建了基于Web的应用程序。托管网址工作正常,但是当我使用phonegap 2.8.1在Windows Phone 7中运行它时,它无法运行。它调用错误函数。

这是我的代码:

<!DOCTYPE html>
<html lang="en">

    <head>
    <meta http-equiv="Content-Type" content="text/html; 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" />
        <meta charset="utf-8">
        <title></title>
        <!-- CSS -->
        <link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
        <link rel="stylesheet" href="assets/css/style.css">     
        <script type="text/javascript" charset="utf-8" src="cordova-2.8.1.js"></script>
        <script type="text/javascript" src="assets/js/jquery-1.8.3.min.js"></script>
        <script type="text/javascript">

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


    function signclick() {


        var validate = true;
        var usv = document.getElementById('username').value;
        var psv = document.getElementById('password').value;
        var emv = document.getElementById('email').value;
        if (usv == '') {
            if (psv != '') {
                document.getElementById('validatepass').innerHTML = "";
            }
            if (emv != '') {
                document.getElementById('validateemail').innerHTML = "";
            }
            document.getElementById('validateuser').innerHTML = " - Please Enter a User Name";
            validate = false;
        }
        if (emv == '') {
            if (usv != '') {
                document.getElementById('validateuser').innerHTML = "";
            }
            if (psv != '') {
                document.getElementById('validatepass').innerHTML = "";
            }
            document.getElementById('validateemail').innerHTML = " - Please Enter Email ID";
            validate = false;
        }
        if (psv == '') {
            if (usv != '') {
                document.getElementById('validateuser').innerHTML = "";
            }
            if (emv != '') {
                document.getElementById('validateemail').innerHTML = "";
            }
            document.getElementById('validatepass').innerHTML = " - Please Enter a Valid Password";
            validate = false;
        }
        if (usv != '' && psv != '' && emv != '') {
            document.getElementById('validateuser').innerHTML = "";
            document.getElementById('validateemail').innerHTML = "";
            document.getElementById('validatepass').innerHTML = "";
            var make = { "Email": emv, "Username": usv, "Password": psv };

            $.ajax({
                type: "POST",
                url: create_user,
                data: JSON.stringify(make),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (result) {
                    window.location = "Login.html";
                },
                error: function (error) {
                    $("#alert").show();
                    document.getElementById('alert').innerHTML = "<b>Connection Failed!</b> Please Check Your Configurations";
                }
            });
        }


    }


</script> 

    </head>

    <body>
<div class="container">
        <div class="header">

                <div class="row">
                    <div class="logo span4">
                    <a href="http://www.texpoglobal.com"><img src="assets/img/texpo.png" width="70" height="70" align="left" style="margin-left:15px;margin-top:8px;"/></a>

                    </div>
                    <div class="links span8">
                        <a class="home" href="index.html" rel="tooltip" data-placement="bottom" data-original-title="Home"></a>
                    </div>
                </div>
            </div>


        <div class="register-container container">
            <div class="row">

                <div class="register span6" style="min-width:230px;">
                    <form action="" method="post">
                        <h2>REGISTER TO <span class="red"><img src="assets/img/texpo2.png"  width="90px" height="60px" /> </span></h2>
                         <div id="alert" style="display:none;"class="alert alert-error"></div>
                        <label for="username">Username<span id="validateuser" class="valid" ></span></label>
                        <input type="text" id="username" name="username" placeholder="choose a username..." required>
                        <label for="email">Email<span id="validateemail" class="valid" ></span></label>
                        <input type="text" id="email" name="email" placeholder="enter your email..." required>
                        <label for="password">Password<span id="validatepass" class="valid" ></span></label>
                        <input type="password" id="password" name="password" placeholder="choose a password..." required>
                        <button type="button" onclick="signclick()" >REGISTER</button>


                    </form>
                </div>
            </div>
        </div>
</div>
        <!-- Javascript -->
        <script src="assets/js/jquery-1.8.2.min.js"></script>
        <script src="assets/bootstrap/js/bootstrap.min.js"></script>
        <script src="assets/js/jquery.backstretch.min.js"></script>
        <script src="assets/js/scripts.js"></script>
        <script src="assets/js/config.js"></script>
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript">
            app.initialize();
        </script>
    </body>

</html>

我真的不知道是什么问题。它适用于Android,但不适用于Windows手机。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

问题已解决!!!!

我必须在$.support.cors = true;来电之前添加$.ajax。问题是我是从跨平台向WebService发布信息。

以下是代码:

$.support.cors = true;
    $.ajax({
                    type: "POST",
                    url: create_user,
                    data: JSON.stringify(make),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (result) {
                        window.location = "Login.html";
                    },
                    error: function (error) {
                        $("#alert").show();
                        document.getElementById('alert').innerHTML = "<b>Connection Failed!</b> Please Check Your Configurations";
                    }
                });

如果这不是好的做法,我不会,但它帮助了我。玩得开心。