javascript - How to parse Json object in Jquery - Stack Overflow

I have a little experience in JSON I did it on my Android application now using JSON again in my webpag

I have a little experience in JSON I did it on my Android application now using JSON again in my webpage as AJAX reponse, I did research about ajax and found a tutorial to get my data in database using JSON so I tried but I didn't know how to parse the object.

My Jquery Code.

$.ajax({
    type: 'GET',
    dataType: 'json',
    url: 'functions/json.php',
    success: function(response){
   var json = $.parseJSON(response);
   alert(json.firstname) //where my response is $response['firstname']
},
error: function(data){
   var json = $.parseJSON(data);
   alert(json.error);
}
});

Using the php I echo the jsonArray as json_encode and heres the json output

{"id":"2","firstname":"john","lastname":"Doe"}

using google chrome console i got this error

Uncaught SyntaxError: Unexpected token o in JSON at position 1
at JSON.parse (<anonymous>)

when the function response output as alert(reponse) output was

[object Object]

I have a little experience in JSON I did it on my Android application now using JSON again in my webpage as AJAX reponse, I did research about ajax and found a tutorial to get my data in database using JSON so I tried but I didn't know how to parse the object.

My Jquery Code.

$.ajax({
    type: 'GET',
    dataType: 'json',
    url: 'functions/json.php',
    success: function(response){
   var json = $.parseJSON(response);
   alert(json.firstname) //where my response is $response['firstname']
},
error: function(data){
   var json = $.parseJSON(data);
   alert(json.error);
}
});

Using the php I echo the jsonArray as json_encode and heres the json output

{"id":"2","firstname":"john","lastname":"Doe"}

using google chrome console i got this error

Uncaught SyntaxError: Unexpected token o in JSON at position 1
at JSON.parse (<anonymous>)

when the function response output as alert(reponse) output was

[object Object]
Share Improve this question edited Apr 8, 2017 at 14:38 Raize Tech asked Apr 8, 2017 at 14:37 Raize TechRaize Tech 452 silver badges9 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 6

Don't parse it. You've told jQuery to:

dataType: "json"

So response is the parsed object, not JSON. Just use it directly:

$.ajax({
    type: 'GET',
    dataType: 'json',
    url: 'functions/json.php',
    success: function(response){
        alert(response.firstname);
    },
    error: function(data) {
        // `data` will not be JSON
    }
});

Also note that the first parameter to the error callback will not be JSON or the result of parsing JSON in the error callback.

See the documentation for details.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742382903a4433475.html

相关推荐

  • javascript - How to parse Json object in Jquery - Stack Overflow

    I have a little experience in JSON I did it on my Android application now using JSON again in my webpag

    8小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信