[Front]jsonp

Source: Internet
Author: User

Jsonp

Before steamed a few Ajax crabs, taste good, but? Although the crab is good, the meat is too little!

Ajax appears to solve the page does not refresh the situation with the background to communicate with the data exchange, but there is a fatal flaw, he is not cross-domain.

cross-domain: <== problem point to the left

Anyway, the port number. Any place in front of the colon is a cross-domain request.

We know that the SRC script tag of the IMG tag SRC is capable of cross-domain request data, so I do not know who is the great God, the whimsical curve to save the nation, using the script tag for background data acquisition,

Of course, this script tag is generated dynamically with the JS script, for example, a chestnut

First of all, I'm going to write a script.

1.js

Fundemo ({"Result": "Remote Data"});

Write an HTML file again

1.html

<!DOCTYPE HTML><Head>    <title>1</title>    <Scripttype= "Text/javascript">    varFundemo= function(data) {alert (data.result);    }; </Script>    <Scripttype= "Text/javascript"src= "Http://aaa.com/1.js"></Script></Head><Body> </Body></HTML>

function Fundemo (data) {alert (data.result);} The same declaration is true for functions!!!

is actually in the HTML file to declare a name called, Fundemo function, which has a parameter

Then, executes a remote (cross-domain) JS code, called the function, remote JS to provide data, as a parameter

Fundemo ({"Result": "Remote Data"});

So the new problem, although can get to the remote data, but this function name must be consistent with the function name in the background, the solution is to provide a function name after the SRC parameter,

The background of the program according to the function name of the dynamic generation of this JS file

1.html Modification

<script type= "Text/javascript" src= "http://aaa.com/1.php?&callback=fundemo" ></script>

Behind the scenes can consider the string concatenation of the way to return this JS

1.php

<? PHP Header (' Content-type:application/x-javascript '); $callback=$_request[' Callback ']; Echo $callback (data) {alert (data.result);} " >

I don't know if PHP is written to =.=.

That's it.

jquery inside the JSONP is placed in the Ajax inside, but really half a dime relationship wood has AH!!

Crabs can eat, shrimp can also eat, are steamed, the same practice. But they really are not a thing!!!

Use JSONP in Ajax

$.ajax ({type:"Get", URL:"1.php", DataType:"Jsonp", Jsonp:"Callback",//The parameter name passed to the request handler or page to obtain the name of the JSONP callback function (default: Callback)Jsonpcallback: "Success_jsonpcallback",//Custom JSONP callback function name, default to jquery auto-generated random function nameSuccess:function(JSON) {//Do something}, Error:function(){                //Do something            }        });

[Front]jsonp

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.