Making cross-domain requests with jQuery
The previous recipe demonstrated the use of a PHP file as a proxy for querying cross-domain URLs. This recipe will show the use of JSONP to query cross-domain URLs from jQuery itself.
We will create an example that will search for the videos from YouTube and will display them in a list. Clicking on a video thumbnail will open a new window that will take the user to the YouTube website to show that video.
The following screenshot shows a sample JSON response from YouTube:
Getting ready
Create a folder named Recipe2
inside the Chapter9
directory.
How to do it...
Create a file inside the
Recipe2
folder and name it asindex.html
. Write the HTML code to create a form with a single field query and a DIV withresults
ID inside which the search results will be displayed.<html> <head> <title>Youtube Video Search</title> <style type="text/css"> body {font-family:"Trebuchet MS",verdana,arial;width:900px;} fieldset...