Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
How should i return my JSON to AJAX call
Old 06-21-2011, 05:27 AM How should i return my JSON to AJAX call
numbenator's Avatar
Webmaster Talker

Posts: 523
Location: London
Trades: 0
HI everyone.

I have a ajax call to a php script that returns a JSON list.

as below

Code:
$.ajax({
				  url: 'getRestaurants.php',	
				  dataType: "json",
				  success: function(data){
					  msgObj = jQuery.parseJSON(data);
				      alert(msgObj.Juicy);
				  },
				  error: function(e, xhr){				      
					  alert("fail " + xhr);
				  }
				});
my php ( getRestaurants.php) is simply ::

Code:
$aFruits = array (    "Juicy"     => "Grapes",
                      "Colourful" => "Dragon Fruit");  
$szJsonEncoded = json_encode($aFruits);  
echo $szJsonEncoded;

BUT when i alert the parsed data, alert(msgObj.Juicy); I get an error
that "msgObj is NULL"

Could anyone see what i am doing wrong please.


Thanks very much

Steve
__________________

Please login or register to view this content. Registration is FREE
numbenator is offline
Reply With Quote
View Public Profile Visit numbenator's homepage!
 
 
Register now for full access!
Old 06-21-2011, 06:11 AM Re: How should i return my JSON to AJAX call
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
I'm pretty sure you should not try to parse the data yourself. jQuery will do that for you, since you specified json as data type. So just remove the line where you parse the data, and alert data.Juicy instead.

And for the record, should you ever be in actual need of parsing any data manually (which I had to do once, when using a plugin that didn't) you'll have to enclose it in a try-catch block, as in

Code:
try {
   json = jQuery.parseJSON(data);
   alert(json.Juicy);
} catch (e) {
   alert('Could not parse json data');
}
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is online now
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 06-21-2011, 08:18 AM Re: How should i return my JSON to AJAX call
numbenator's Avatar
Webmaster Talker

Posts: 523
Location: London
Trades: 0
Cheers for your reply
I actually found the $.getJSON which pretty much does as i require.

Cheers
__________________

Please login or register to view this content. Registration is FREE
numbenator is offline
Reply With Quote
View Public Profile Visit numbenator's homepage!
 
Reply     « Reply to How should i return my JSON to AJAX call
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.13228 seconds with 12 queries