javascript - 'Undefined' when trying to access json response -


my flask app has code returns following:

return json.dumps({'status': 'ok','url': 'www.blahg.com'})               

my javascript code looks this:

$(function() {   $('#mainbutton').click(function() {         $.ajax({             url: '/buttonclick',             data: $('form').serialize(),             type: 'post',             success: function(response) {                 console.log(response);                 console.log(response.url);             },             error: function(error) {                 console.log(error);             }         });     }); }); 

the first console log looks correct: {"status": "ok", "url": "www.blahrg.com"}, when try access url entry, 'undefined' output. doing wrong?

you can use datatype have jquery parse you:

$(function() {   $('#mainbutton').click(function() {         $.ajax({             url: '/buttonclick',             data: $('form').serialize(),             type: 'post',             datatype: 'json', // bit here             success: function(response) {                 console.log(response);                 console.log(response.url);             },             error: function(error) {                 console.log(error);             }         });     }); }); 

Comments