2

我是android中phonegap开发的新手。我正在开发phonegap 应用程序。我已经使用表单标签在服务器上提交了代码。现在我想找回结果。所以请帮助我如何获得服务器返回的结果。结果采用 JSON 格式。

这是我用来在我的 html 文件中提交表单的代码。

    <form id="signUpform" action="http://web1.xxx.com/sss/signup.php" method="post" 
    onsubmit="return( validate() );">
    <div style="float:left; width: 100%; height: inherit;">
        <h3>New Client</h3>
    </div>
    <br />
    <div style="float:left; width: 80%; height: inherit; margin-top: 5px;">
        <div style="float:left; width: 40%; height: inherit; margin-top: 5px; padding-top: 8px;">
             <label>UserName </label></div>
        <div style="float:right; width: 60%; height: inherit;">
            <input id="UserUsername" type="text" name="username" /></div>
    </div>
    <br />  
    <div style="float:left; width: 80%; height: inherit; margin-top: 5px;">
        <div style="float:left; width: 40%; height: inherit; margin-top: 5px; padding-top: 8px;">
            <label>Password </label></div>
        <div style="float:right; width: 60%; height: inherit;">
            <input id="pass" type="text" name="password" /></div>
    </div>
    <br />
    <div style="float:left; width: 80%; height: inherit; margin-top: 5px;;">
        <div style="float:left; width: 40%; height: inherit; margin-top: 5px; padding-top: 8px;">
            <label>   Confirm Password </label></div>
        <div style="float:right; width: 60%; height: inherit;">
            <input id="cpass" type="text" name="confirm_password" /></div>
    </div>
    <br />
    <div style="vertical-align:middle; float:left; width: 80%; height: inherit; margin-top: 5px;">
        <div style="float:left; width: 40%; height: inherit; margin-top: 5px; padding-top: 8px;">
            <label> Email ID </label></div>
        <div style="float:right; width: 60%; height: inherit;">
            <input id="UserEmail" type="text" name="email" /></div>
    </div>
    <br />
    <div style="float:left; width: 80%; height: inherit; margin-top: 5px;">
        <div style="float:right; width: 100%; height: inherit;">
            <input type="submit" value="Get Started"/> </div>
    </div>
</form>   

表单在服务器上提交。我用 JSON fromat 取回结果。结果是:

{"UserID":"220"} 

我提交表格后,这将在设备上打印。

所以请帮助我如何获得这个价值。

提前致谢

4

3 回答 3

2
 var urlStr = URL1 + "username=" + username + "&password=" + password;

 jQuery.support.cors = true;
 $.ajax({
     type: "GET",
     crossDomain: true,
     contentType: "application/json; charset=utf-8",
     url: urlStr,
     data: "{}",
     dataType: "json",
     timeout: 5000,
     success: ajaxCallSucceed,
     error: ajaxCallFailed
 });

function ajaxCallSucceed(json){
 //  success
 }

function ajaxCallFailed(json){
 // failed
}
于 2013-07-02T05:39:50.983 回答
1
      jQuery.ajax({
                    url : MyPostUrl,
                    type : "GET",
                    dataType : "json",
                    success : function(resultvalue) {

                        var val = JSON.stringify(resultvalue);
                        for ( var i = 0; i < resultvalue.Result.length; i++) {
                            finalresult = resultvalue.Result[i].result;
                            //alert(finalresult);
          //u can get your value here.
          //u need to use this loop to get value.
          //As it gives here {"result":"1"}
          //If your  "userID" is within "result" in json.Then u need to set another loop here.else just replace "result" with your "userID" key at (resultvalue.Result[i].result;). 
                        }
                        $.mobile.hidePageLoadingMsg();


                    },
                    error : function(e) {
                        //navigator.notification.alert("Request Failed.Please check Net connection");
                        navigator.notification.confirm(
                                'Request Failed.Please check Net connection', // message
                                alertDismissed, // callback to invoke with index of button pressed
                                'Title', // title
                                'OK' // buttonLabels
                        );

                        $.mobile.hidePageLoadingMsg();
                    }

                });
于 2013-07-02T05:23:26.683 回答
1

要向远程服务器发送请求,请使用jQuery.ajax

示例代码是

function FetchData() {
    $.ajax({
        async: false,
        type: "POST",
        url: "Your_URL",
        dataType: "json",
        success: function (data, textStatus, jqXHR) {


            $.each(data, function (i, object) {
                alert(obj.Data);
            });

        },
        error: function () {
            alert("There was an error loading the feed");
        }
    });
}

调用onclick按钮的此功能。

希望有帮助。

于 2013-07-02T05:32:22.300 回答