简体   繁体   English

javascript-如何在电话间隙中击中URL并使用jquery解析结果?

[英]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: 我在phonegap中有一项任务,即注册页面,如果使用单击注册页面意味着值将命中url,并且用户是否可用,则意味着它将数据返回为json。我使用了以下代码:

$(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"}] reg.php的响应如下: [{"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"}] “这样的返回数据: [{"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]); 
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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