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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法使用PhoneGap在Windows Phone 7中调用Ajax方法相关的知识,希望对你有一定的参考价值。

我使用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手机。任何帮助,将不胜感激。

答案

问题解决了!!!!

我只需要在$.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";
                    }
                });

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

以上是关于无法使用PhoneGap在Windows Phone 7中调用Ajax方法的主要内容,如果未能解决你的问题,请参考以下文章

Phonegap 使用 GCM 向 ios 推送通知

无法在 Windows phone 和 iOS 中安装 Phonegap 构建的应用程序

jQuery Mobile无法在PhoneGap Windows Phone 8中运行

Phonegap LocalStorage - 是不是需要文件系统权限

使用 phonegap 构建服务的插件 plugin.google.maps 在 ios 上不起作用

Phonegap、Corona 或 Monotouch 是不是可以在 iOS 上进行位置感知通知?