Welcome Guest, Not a member yet? Register   Sign In
Serving JSON & JSONP: Adding ?callback=function to a URL
#1

[eluser]tharri[/eluser]
I have set up my server so that when it gets a request for this:

http://www.domain.com/feed/json

... it will correctly serve JSON data.

If there is a request for this:

http://www.domain.com/feed/json?callback=something

... it should serve JSONP data -- that is, JSON data wrapped in a function called "something".

The problem is, how do I get the value of callback? When I print_r($_GET), it is empty.

The only way I've been able to figure out is to use the PHP explode function to convert server('QUERY_STRING') into an array and check each key/value pair for callback. Is there an easier way?

And yes, I know I could change the URL structure, but it has basically become a "standard" that a json page can be converted to jsonp by adding "?callback=function" to the end of the URL. For example, see these two links:

http://twitter.com/users/buzzfeed.json
http://twitter.com/users/buzzfeed.json?c...ckfunction

The first is JSON. The second is JSONP. Many javascript libraries, including JQuery (I think) use that syntax, so I'd like to be able to support it.

Also, I don't want to set $config[‘enable_query_strings’] = TRUE because everywhere else on the site, I don't want it to accept query strings. I want it to use the standard domain.com/controller/function/id format.




Messages In This Thread
Serving JSON & JSONP: Adding ?callback=function to a URL - by El Forum - 05-09-2012, 07:29 PM



Theme © iAndrew 2016 - Forum software by © MyBB