rjanjic / PHP_JSONP_Response

Encode and generate a response to JSONP request

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PHP_JSONP_Response

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.

Example

PHP

require 'JSONP.class.php';

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

jQuery call

$.ajax({
	// 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 (data.foo == 'bar') {
			alert('Baz!');
		}
	}
});

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);

About

Encode and generate a response to JSONP request


Languages

Language:PHP 77.1%Language:JavaScript 22.9%