Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get javascript object from ajax request

How do I create a javascript object from an AJAX response?

This is what I'm currently doing:

PHP:

<?
echo '{status:"1", site:"example.com"}';
?>

JS:

success:function(data_response){
    var object = eval( data_response );
    console.log(object.url);
    }});

I'm currently getting "undefined" on the console. How is this done?

like image 575
lisovaccaro Avatar asked Jul 19 '26 09:07

lisovaccaro


1 Answers

Set the dataType of the ajax request to json, and the data_response will be an object already parsed to.

Or you could use $.getJSON also.

like image 76
xdazz Avatar answered Jul 20 '26 21:07

xdazz