PHP Classes
elePHPant
Icontem

File: README.md

Recommend this page to a friend!
  Classes of Radovan Janjic  >  PHP JSONP Response  >  README.md  >  Download  
File: README.md
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:
Date: 4 years ago
Size: 1,262 bytes
 

 

Contents

Class file image Download

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