ajlisowski Posted May 11, 2010 Share Posted May 11, 2010 Hey everybody, Ive been messing around with ajax for a year or so now. However, one thing I feel like I am always sort of "hacking" together is the way I pass data back to javascript. A lot of times I will simply pass back a string with some custom deliminator (ex: ** or ||) however this does not seem to be the most effecient or "correct" way of doing things. Ive reused a ton of code, so a lot of my projects all run off the same basic handlers which explode the deliminator, check the first result for a success or error code and then handle the rest of the array as data. Is this wrong? Should I switch to using JSON or XML? Or is what I am doing acceptable? Quote Link to comment Share on other sites More sharing options...
andrewgauger Posted May 11, 2010 Share Posted May 11, 2010 With your topic title being "best practice" I would advise XML. However, JSON is generally easier to parse. http://www.php.net/manual/en/function.json-decode.php Makes it pretty easy to use. Quote Link to comment Share on other sites More sharing options...
Mchl Posted May 11, 2010 Share Posted May 11, 2010 Why XML would be 'best practice'? Just because it's an 'X' in 'Ajax' ? The argment between proponents of XML and JSON is heated, but I can't really see actual arguments on favor of either. I like JSON more, but that might be because I work with ExtJS mostly, which has some splendid tools for JSON. Quote Link to comment Share on other sites More sharing options...
andrewgauger Posted May 11, 2010 Share Posted May 11, 2010 XML was first, and that is why I quote as best practice. I do not support it as the best product. Quote Link to comment Share on other sites More sharing options...
Mchl Posted May 11, 2010 Share Posted May 11, 2010 Argument from age? Doesn't seem valid for me Anyway, I think the onswer to OP's question is to use some standarised fromat, like XML or JSON or even php serialised data. Which exactly is the best for given application, depends on specific rquirements for this applicaiton. Quote Link to comment Share on other sites More sharing options...
ajlisowski Posted May 12, 2010 Author Share Posted May 12, 2010 For the sake of knowledge I was looking into how to pass an xml object from JS to PHP. I know that you can set your header to txt/xml in the httprequest object before sending it. I monitored my progress in firebug and can see that the POST successfully contains the xml object, but how do I actually access that in the php file? $_REQUEST is empty, which makes sense since I didnt send it as a post param. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.