Last active
December 17, 2015 23:29
-
-
Save gongzhitaao/5689300 to your computer and use it in GitHub Desktop.
How jsonp works, detailed explanation plz refer to http://www.onlinesolutionsdevelopment.com/blog/web-development/javascript/jsonp-example/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="text/javascript">// <![CDATA[ | |
function callTheJsonp() | |
{ | |
// the url of the script where we send the asynchronous call | |
var url = "http://localhost/utils/jsonp/ajax.php?callback=parseRequest"; | |
// create a new script element | |
var script = document.createElement('script'); | |
// set the src attribute to that url | |
script.setAttribute('src', url); | |
// insert the script in out page | |
document.getElementsByTagName('head')[0].appendChild(script); | |
} | |
// this function should parse responses.. you can do anything you need.. | |
// you can make it general so it would parse all the responses the page receives based on a response field | |
function parseRequest(response) | |
{ | |
try // try to output this to the javascript console | |
{ | |
console.log(response); | |
} | |
catch(an_exception) // alert for the users that don't have a javascript console | |
{ | |
alert('product id ' + response.item_id + ': quantity = ' + response.quantity + ' & price = ' + response.price); | |
} | |
} | |
// ]]></script> | |
<span onclick="callTheJsonp()">click here to make the jsonp call</span> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment