How to read AJAX response variable?

henrywright

The following is the response I'm receiving in my AJAX success function:

"{"success":true,"data":{"id":1841,"title":"atitle","filename":"filename.jpg","url":"http:\/\/example.com\/wp-content\/uploads\/2014\/11\/filename.jpg","link":"http:\/\/example.com\/?attachment_id=1841","alt":"","author":"21","description":"","caption":"","name":"filename-39","status":"inherit","uploadedTo":0,"date":1415555051000,"modified":1415555051000,"menuOrder":0,"mime":"image\/jpeg","type":"image","subtype":"jpeg","icon":"http:\/\/example.com\/wp-includes\/images\/media\/file.png","dateFormatted":"November 9, 2014","nonces":{"update":"b832c2939d5","delete":"83dda46357e","edit":"51ac41b11c6"},"editLink":"http:\/\/example.com\/wp-admin\/post.php?post=1841&action=edit","meta":false,"authorName":"Some One","filesizeInBytes":10755,"filesizeHumanReadable":"11 kB","sizes":{"thumbnail":{"height":90,"width":90,"url":"http:\/\/example.com\/wp-content\/uploads\/2014\/11\/filename-90x90.jpg","orientation":"landscape"},"full":{"url":"http:\/\/example.com\/wp-content\/uploads\/2014\/11\/filename.jpg","height":260,"width":236,"orientation":"portrait"}},"height":260,"width":236,"orientation":"portrait","compat":{"item":"","meta":""}}}"

I'm trying to update the src attribute of a particular image on my page using the data that is returned in this response. For example:

$( '#myimage' ).attr( 'src', response.data.url );

The problem is, I get the error Uncaught TypeError: Cannot read property 'url' of undefined

I'm sure response.data.url is wrong. How can I get the URL from the response so that I can update the image's src attribute?

Ernesto

yo could use

x=$.parseJSON(response)

and this will convert the json string to a valid json objet, if there is an error will throw an exception you can use try{}catch(e){} to fix it

try{
var x=$.parseJSON(response);
}catch(e){
console.log(e);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to select a variable from an AJAX response

From Java

How to read an array which is received as Ajax response text in JavaScript

From Dev

how to read ajax response with multiple objects in laravel 5.2

From Dev

How to get the ajax response from success and assign it in a variable using jQuery?

From Dev

How to set ajax response.responseText as global JS variable

From Dev

How to get the ajax response from success and assign it in a variable using jQuery?

From Dev

How to get response text from ajax callback in a variable using ExtJS

From Dev

save ajax response to jquery variable

From Dev

How to read JSON response?

From Dev

how to read javascript variable from ajax success callback function

From Dev

how to read javascript variable from ajax success callback function

From Dev

How to alert ajax response

From Dev

How to clear ajax response

From Dev

Ajax response how to access

From Dev

jQuery read array from ajax response

From Dev

Jquery - Read HTTP response from a promise/deferred Ajax response

From Dev

Using variable from ajax response as json

From Dev

jQuery Ajax passing response variable to function

From Dev

Using ajax json response in an angular scope variable

From Dev

Pass Ajax variable value as response data name

From Dev

How to read the response from a NewSingleHostReverseProxy

From Dev

How to read response headers in angularjs?

From Dev

How to read response headers with $resource?

From Dev

How to read a JSON response in Unity?

From Dev

how to get $.ajax settings in response?

From Dev

How to get Ajax response in Go

From Dev

Django template - ajax response - how to?

From Dev

How to handle Ajax JSON response?

From Dev

How to catch ajax error response?

Related Related

  1. 1

    How to select a variable from an AJAX response

  2. 2

    How to read an array which is received as Ajax response text in JavaScript

  3. 3

    how to read ajax response with multiple objects in laravel 5.2

  4. 4

    How to get the ajax response from success and assign it in a variable using jQuery?

  5. 5

    How to set ajax response.responseText as global JS variable

  6. 6

    How to get the ajax response from success and assign it in a variable using jQuery?

  7. 7

    How to get response text from ajax callback in a variable using ExtJS

  8. 8

    save ajax response to jquery variable

  9. 9

    How to read JSON response?

  10. 10

    how to read javascript variable from ajax success callback function

  11. 11

    how to read javascript variable from ajax success callback function

  12. 12

    How to alert ajax response

  13. 13

    How to clear ajax response

  14. 14

    Ajax response how to access

  15. 15

    jQuery read array from ajax response

  16. 16

    Jquery - Read HTTP response from a promise/deferred Ajax response

  17. 17

    Using variable from ajax response as json

  18. 18

    jQuery Ajax passing response variable to function

  19. 19

    Using ajax json response in an angular scope variable

  20. 20

    Pass Ajax variable value as response data name

  21. 21

    How to read the response from a NewSingleHostReverseProxy

  22. 22

    How to read response headers in angularjs?

  23. 23

    How to read response headers with $resource?

  24. 24

    How to read a JSON response in Unity?

  25. 25

    how to get $.ajax settings in response?

  26. 26

    How to get Ajax response in Go

  27. 27

    Django template - ajax response - how to?

  28. 28

    How to handle Ajax JSON response?

  29. 29

    How to catch ajax error response?

HotTag

Archive