0


is it not necessary to call the json_encode function in the php page, with an Ajax method?
for example, i found this code :

$.post('updatecoords.php', 'data='+$.toJSON(order), function(response){
if(response=="success") 

and at the end of the php page :

echo "success";
?> 

So here is "echo", instead of json_encode ($array). It's one or another? or is there any rule? Thanks for the answer! ;)

2
  • Could you better explain what you are trying to accomplish? And there is no $.toJSON method, are you using a plugin? Commented Apr 24, 2011 at 14:06
  • @NeXXeuS : it comes from there : net.tutsplus.com/tutorials/javascript-ajax/… i thought the toJSON strange also, but since i'm quite a beginner... you can see the code in the middle of the page Commented Apr 24, 2011 at 17:38

1 Answer 1

2

JSON is only a format for passing data around. In AJAX calls it makes more sense to encode the data as JSON and pass it to the browser because a simple eval of the JSON string is all that has to be done by the clientside javascript. It is not necessary to use JSON for AJAX calls though. You can return anything from full fledged HTML pages to a simple string as the response to an AJAX call. You just have to make sure that your client side code can handle the response correctly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.