简体   繁体   中英

How to hit the url in phone gap and parse the result using jquery?

am having one task in phonegap,that is registration page if the use click the registration page means the values are going to hit the url and if the users is alreay avaiable or not means it returns the data as json.the following code i used:

$(document).ready(function(){
    $('button').click(function(){
        var username = $('#uid').val();
        var email=$('#email').val();
        var password=$('#pass').val();
        var firstname = $('#first').val();
        var lastname=$('#last').val();
        var dob=$('#dob').val();

        var param = 'username=' + username + '&email=' + email +'&password='+password+'&firstname='+firstname+'&lastname='+lastname+'&dob='+dob;
        alert("hi");
        alert(param);
        $.ajax({
            url: "http://demourl/api/reg.php?",
            type: 'GET',
            data: param,
            success: function(result) {
                alert('success');
            },
            error: function(result) {
                alert("jQuery Error:" + result.statusText);
            }
        });
    });
});

response from the reg.php is like this: [{"error":"Email is taken"}]

here i dont know how to parse the data?please help me

Check the below code

$(document).ready(function(){
    $('button').click(function(){
        var username = $('#uid').val();
        var email=$('#email').val();
        var password=$('#pass').val();
        var firstname = $('#first').val();
        var lastname=$('#last').val();
        var dob=$('#dob').val();

        var param = 'username=' + username + '&email=' + email +'&password='+password+'&firstname='+firstname+'&lastname='+lastname+'&dob='+dob;
        alert("hi");
        alert(param);
        $.ajax({
            url: "http://demourl/api/reg?",
            type: 'GET',
            data: param,
            success: function(result) {
                var parseJson = $.parseJSON(result);
                console.log(result);
            },
            error: function(result) {
                var parseJson = $.parseJSON(result);
                alert("jQuery Error:" + parseJson.error);
            }
        });
    });
});

You could do something like this

...
error: function(result) {
  var parsedJSON = $.parseJSON(result);
  alert(parsedJSON.error);
}

According to my understanding you have asked for

" the return data like this: [{"error":"Email is taken"}]

here i dont know how to parse the data? ..."

So accordingly, may may be expecting this one,

var data = {"error":"Email is taken"}; 

for(var key in data)
{ 
  console.log('key name: ' + key + ' value: ' + data[key]); 
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM