A standard to PHP AJAX responses

At Blue Bay Travel, we use AJAX a lot to create both fluid and interactive websites and applications. Since we've had a few developers, there are a lot of different ways of outputting the callback page content, sometimes you might not use JSONP (no callback etc) other times you might, so you never really know what you'll get without trawling through the code.

This is where I've decided to create a standard response format (which I'll now call SRF) it gives you the flexibility of deciding what kind of response you get. By this, I mean whether it's a successful, erroneous or informational response. In fact there are currently four types of responses:

  • MSG
  • INFO
  • ERR
  • WARN

All of which have different meanings. MSG for example is when the requested function works correctly, everything has gone well. INFO is for informing the user about something, maybe an AJAX contact form has invalid data? ERR is an error, nothing happened, something broke. And finally WARN is for when you might be unable to determine if something has happened, or some setting may affect the expected outcome.

The class also allows you to state whether you're making a JSONP request, if you are, you can state what callback name to use. You can also change the callback name if needs be by using a simple method.

There is more documentation on the GitHub page, and I'll eventually get around to making proper documentation - that actually explains stuff.

For now, the code is available at github.com/jbrooksuk/PHP-AJAX-Responses but that will change soon!

comments powered by Disqus