user2500916
user2500916

Reputation:

Get JSON Object after submit the form in phonegap

I'm new in phonegap development in android. i'm working on phonegap application. I have subbmited the code on the server using the form tag. Now i want to get the result back. So please hlep me how to get the result which is returned by the server. the result is in the form of JSON.

Here is my code that i'm using to submit the form in my html file.

    <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>   

form is submited on the server. and i get back the result in JSON fromat. Result is :

{"UserID":"220"} 

this will print on the device after i submitted the form.

So please help me how i can get this value.

Thanks in advance

Upvotes: 2

Views: 1059

Answers (3)

Gurpreets11
Gurpreets11

Reputation: 2351

 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
}

Upvotes: 2

Amol Chakane
Amol Chakane

Reputation: 1511

To send a request to remote server use jQuery.ajax

Sample code is

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");
        }
    });
}

Call this function onclick of your button.

Hope that helps.

Upvotes: 1

Bhoomika Brahmbhatt
Bhoomika Brahmbhatt

Reputation: 7415

      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();
                    }

                });

Upvotes: 1

Related Questions