Brief Introduction to jsonp usage summary_jquery-js tutorial

Source: Internet
Author: User
Jsonp (JSONwithPadding) is a type of json & quot; usage mode & quot;, which allows the webpage to retrieve data from other domain names (websites), that is, to read data across domains, this article briefly introduces the use of jsonp. For more information, see Jsonp (JSON with Padding), which is a "usage mode" of json and allows the webpage to access other domain names (websites) obtain the data, that is, read data across domains.

Why do we need a special technology (JSONP) to access data from different domains (websites? This is because of the same-origin policy.

Same-origin policy, a well-known security policy proposed by Netscape, is now used by all browsers that support JavaScript.

First, jsonp is a json cross-domain object.

The principle is to bypass the same-origin policy through the cross-origin feature of the script tag.

Tested experiment:

Sender:

$. Ajax ({type: "post", url: "ajax. php ", dataType:" jsonp ", jsonp:" callback ", // The parameter name that is passed to the request handler or page to obtain the jsonp callback function name (default: callback) jsonpCallback: "jsonpcallback", // custom jsonp callback function name. By default, the random function name automatically generated by jQuery is success: function (json) {alert ('success') ;}, error: function () {alert ('failed ');}});

Server (php ):

<? Php $ data = "aaa"; $ callback =$ _ GET ['callback']; echo $ callback. '('. json_encode ($ data ). ')'; exit;?>

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.