PHP Classes


Recommend this page to a friend!
  Classes of Radovan Janjic   PHP JSONP Response   Download  
Role: Auxiliary data
Content type: text/markdown
Description: Auxiliary data
Class: PHP JSONP Response
Encode and generate a response to JSONP request
Author: By
Last change: Update of
Date: 1 month ago
Size: 1,262 bytes


Class file image Download


Encode and generate a response to JSONP request

This class can encode and generate a response to JSONP request.

It generates JavaScript code to return JSON encoded variable value as response to a JSONP request.

The generated JavaScript may either invoke a callback function with a name defined by a request variable, or assign a JavaScript variable with the JSON encoded variable value.



require 'JSONP.class.php';

// Print output with correct headers informations
JSONP::output(array('foo' => 'bar'));

jQuery call

	// Call to url
	url: '/path/to/example.php',
	// Data Type
	dataType: 'jsonp',
	// Send data
	data: {
	  foo: 'bar',
	  baz: 'qux'
	// On success call
	success: function(data) {
		// Do stuff with data object 
		if ( == 'bar') {

Other PHP examples

require 'JSONP.class.php';

// JSON encode data
$json_encoded = JSONP::encode(array('foo', 'bar'));

// JSONP string as returned value
$jsonp_encoded = JSONP::output(array('foo', 'bar'), FALSE);

// Print output
JSONP::output(array('foo', 'bar'));

// Print output without headers
JSONP::output(array('foo', 'bar'), TRUE, FALSE);