Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript AJAX Basics (retiring) AJAX Concepts AJAX Response Formats

Raquel Redondo
Raquel Redondo
6,494 Points

AJAX: are responses from AJAX always text? Cant responses be made of more complex data types such as Objects?

In the video: https://teamtreehouse.com/library/ajax-basics/ajax-concepts/ajax-response-formats it is said that responses are made of text, but I want to know if that is always the case or responses can be of any type.

2 Answers

Jacques Wessels
Jacques Wessels
22,557 Points

Hi there,

I'm speaking under correction here, but I think there are two possible types of responses to AJAX requests. One is XML, and the other is a string. If the string is JSON formatted, you can then use the JSON.parse method to construct an actual object based on that string.

Armand van Alphen
Armand van Alphen
16,969 Points

You can even get valid HTML code from an .HTML file to inject into your web-page but this is still a string, Also as mentioned above you can get a JSON formatted string which are objects and arrays converted to a string by PHP Python or other back-end programs which JavaScript is able to convert back to arrays and objects.