Jsonp using Notes

Source: Internet
Author: User

JSONP

Jsonp is a way to implement cross-domain get requests, using the script tag to dynamically load JS files,

Loading JSON data from different source sites to this site, if given a callback function, pass the callback function name to the server side,

The Getjson interface for general AJAX implementation is generated on the server side by generating a call statement with JSON data as the input parameter.

Getjson interface If the request URL is the same as the current URL, it is a generic web site access.

A detailed explanation is given below.

Http://www.cnblogs.com/yuzhongwusan/archive/2012/12/11/2812849.html

Experiment

jsonpserver.php

<? PHP $jsondata = "{symbol: ' IBM ', price:120}";   Echo $_get [' Callback ']. ' (‘. $jsondata. ') '  ?>

Jsonpclient.html

<HTML><Head>         <Scripttype= "Text/javascript"src= "Https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></Script>        <style>        </style></Head> <Body>        <H1>Hello world!</H1>         <inputtype= "text"value= "Test"/>         <inputtype= "button"value= "button"/>         <Scripttype= ' Text/javascript '>             $("[type= ' button ']"). Click (function(){                //JQuery JSONP Support                varURL= "http://127.0.0.1/jsonpServer.php?callback=?"; $.getjson (URL,function(data) {varretmsg= "Symbol:" +Data.symbol+ ", Price:" +Data.price; $("[type= ' text ']"). Val (retmsg);             }); })        </Script></Body></HTML>

Client users access http://localhost/jsonpClient.html,

Click the button to initiate the JSONP request,

GET http://127.0.0.1/jsonpServer.php?callback=jQuery18305268568145111203_1403193771906&_=1403194058203 http/ 1.1
host:127.0.0.1
Connection:keep-alive
Accept: */*
user-agent:mozilla/5.0 (Windows NT 5.1) applewebkit/537.36 (khtml, like Gecko) chrome/33.0.1750.154 safari/537.36
Referer:http://localhost/jsonpclient.html
Accept-encoding:gzip,deflate,sdch
accept-language:zh-cn,zh;q=0.8,en-gb;q=0.6,en;q=0.4

Server-side response content is, visible $.getjson detection URL has callback=? That represents the second argument as a callback function,

But the second function is an anonymous function, so rename the function to a complex long string in case the call is triggered by the response.

http/1.1 OK
Date:thu, June 16:07:38 GMT
server:apache/2.4.7 (Win32) openssl/0.9.8y php/5.4.25
x-powered-by:php/5.4.25
content-length:67
Keep-alive:timeout=5, max=100
Connection:keep-alive
Content-type:text/html

jquery18305268568145111203_1403193771906 ({symbol: ' IBM ', price:120})

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.