Skip to content Skip to sidebar Skip to footer

Php Json_encode To Js

PHP json_encode as AJAX var 'result': [{'id':'139','assettypeid':'3','name':'skin1','body':'skin1.jpg'}] I'm trying to access each property, but I can't: for (var i =0;i < resu

Solution 1:

you need to use JSON.parse

var items = JSON.parse(result)

http://www.json.org/js.html

Solution 2:

UPDATE:

I modified my answer to create a JSON call to a server side file like PHP or Ruby. If you are using jQuery try this instead:

$.ajax({
    url: 'http://url-of-your-server-side.com/server-side-file-name.php',
    dataType: 'jsonp',
    jsonp: 'jsoncallback',
    success: function(data) {
        $.each(data, function(i,item){
            console.log (item.id + item.name + item.body);
        });
    error: function(){
        // execute upon failure
    }

Data is the variable that holds your array provided by your ajax request.

Solution 3:

for(var item in result){
    console.log(item.id, item.name, item.body);
}

Post a Comment for "Php Json_encode To Js"