Skip to content Skip to sidebar Skip to footer

Javascript: How To Test Jsonp On Localhost

Here is the way I test JSONP: I run XAMPP, and in folder htdocs I create a javascript folder . I create json1.json file contain some data to process. After that, I run this html fi

Solution 1:

JSONP is generated on the server's side. The server fetches your data, encodes it as JSON and then wrap a function call around it.

So in your case you can either modify your .json file the following way:

updateSales( { /* your data */ } );

This, however, would have a hardcoded call back method, so it wont react on changes you do at retrieving it.

The other way is to write a small PHP wrapper (or use whatever else you like) and add the function around the JSON, before sending it back to the client.

<?php$json = file( "path/to/your/json" );

  echo$_GET['callback'], '(', implode( '', $json ), ');';

?>

(The later code is by no means for any productive use, but only for local testing!)

Post a Comment for "Javascript: How To Test Jsonp On Localhost"