JSONRequest.post Example
October 26th, 2007 — 4:08 pm —As I wrote in an earlier post we’re looking at JSON for one of our new features in slate. To quote the official JSON site, “JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate.” So far in playing with it I’d have to agree.
The one thing we’re doing a little differently (I think) is that we want to pass the JSON object from the page to the server as opposed from the server to the page. We’ll probably be doing the latter at some point though. Their is a proposal out there for adding JSONRequest to JavaScript proper. In lieu of it actually being there I messed around with a JSON JavaScript library from Andrea Giammarchi. While I worked with PHP for my example (it was just faster) and the JSON PECL package come Rails 2.0 to_json and from_json are both supported by default. Ready for an upgrade, Chris? ;)
So what did I come up with? This. Yup, a super cool method to reverse inputted text :) The JS code isn’t anything incredible:
//Show the info returned from the PHP script
function showJSONInfo(sn, response, error) {
if (response) {
$('revText').innerHTML = "<strong>It worked!</strong><br />Your text reversed: " + response.revText;
new Effect.Highlight('revText', {startcolor:'#ffcc00', endcolor:'#ffffff'});
}
else {
alert(error);
}
}
//Format and send the data to the PHP script
function getJSONInfo() {
var JSONInfo = {"normText":$('normText').value};
try {
JSONRequest.post("json.php", JSONInfo, showJSONInfo);
}
catch(e) {
alert(e);
}
}
Overall the library was pretty easy to use. I had to bang my head against a few things but that’s probably due to my own stupidity than anything else. A couple of things:
- While it’s noted that JSONRequest.js requires the associated error .js file it actually requires JSON.js and it’s associated error .js file as well. Maybe that should have been blindingly obvious to me but it wasn’t.
- I just dealt with the raw POST on the PHP side so, if you want to do the same, you might want to edit line 240 of JSONRequest.php to take out the var name.
- Make sure you’ve got the right content-type. I assume this isn’t a problem in Rails. It should be application/jsonrequest
- On the PHP side make sure you urldecode() that raw POST data. The decode may not be perfect so YMMV.
- Also on the PHP side (which is obviously where I had most of my problems) you can use arrays to hold your JSON data if you, like me, have no clue how to use objects in PHP. So it’s:
json_decode(urldecode($jsonData),true);
Note: I just started looking at JSON a few days ago so if I’ve rehashed things people already know or if I’ve gone into left field doing this ass-backwards feel free to let me know.
About slate
slate is a content management system (CMS) developed using Ruby on Rails focused on rapid production of traditional websites created by WVU Web Services. Read more about why we created slate and a longer list of features of slate. You can also check out a list of sites using slate. If you have questions or comments let us know but if it's a question about open sourcing slate have a look at this article first.Archives
Recent articles
- No Wonder Rails Is Default in Mac OS X Server...
- Good News: An Open Sourced slate Is Coming
- The WVU Open Source License
- Implementation idea: .do templates
- Keeping slate Humming - Part 1
- Campfire for Design & Keeping Your Tag Cloud Running
- Custom configuration settings made easy
- HOW-TO: Add a Gallery to Your Site
- JSONRequest.post Example
- Miscellaneous
Articles
Add comment
You are adding a new comment