Return XML or JSON via AJAX Request

Javascript coding ..

Moderators: macek, egami, gesf

jplus2
New php-forum User
New php-forum User
Posts: 87
Joined: Wed May 16, 2012 4:17 pm

Return XML or JSON via AJAX Request

Postby jplus2 » Thu Sep 06, 2012 4:20 pm

I would like to ask

1. How to return XML or JSON via AJAX Request
2. How can I digest the returned XML or JSON on the JS File that called the AJAX Request

I am using jquery to perform AJAX Request, particularly this way:

$.post('filetoconnecttoserverandgetdata.php',{variabletopass:valueofvariable},function(data){
// this is the call back function to handle the response
});

I am using it on my site to perform basic tasks, such as checking if username or email is still available while a user is registering. So far so good, coz I am using only basic things about

but I have another page on my site, where an admin (this is an admin area) can input the id of the user, then all information of that user, such as email, username, firstname, lastname, age, etc.... will be retrieved from the db and be displayed on that page.


Now I dont want the result back as a very long string, I want the result inside some form of a data structure, and that is either XML or JSON

Please do help me on this. Also

3. not so important question, which one is better? XML or JSON?

I know this is like comparing an apple to an orange, but I want to know the basic difference of the 2, such as, which is much easier to learn, much cleaner syntax, etc.. etc..

Please do provide sample codes, :D

God Speed!

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Return XML or JSON via AJAX Request

Postby johnj » Sat Sep 08, 2012 4:22 am

3. not so important question, which one is better? XML or JSON?


Well, it depends on the need and what are comfortable at.

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Return XML or JSON via AJAX Request

Postby johnj » Sat Sep 08, 2012 4:29 am

I think you can specify the datatype (as json or xml or something else that is allowed) in ajax request. Please read the documentation.

json is hard to read while xml is more friendly to human eyes. But json and javascript belong to the same gang or family and hence json has some advantages/concessions that are given to it by js which is not available to xml - pLease read the documentation.

jplus2
New php-forum User
New php-forum User
Posts: 87
Joined: Wed May 16, 2012 4:17 pm

Re: Return XML or JSON via AJAX Request

Postby jplus2 » Sun Sep 09, 2012 4:57 pm

Thanks johnj, I got it working now, I've used json as per adviced

thank you so much for your thoughts


Return to “JavaScript”

Who is online

Users browsing this forum: No registered users and 1 guest