This is a long time spent on a jquery cross-origin JSON-jquery cross-origin retrieval JSON Data Summary

Source: Internet
Author: User

This is a cross-origin issue that has occurred in the last half of the day. It's better to say it's a dead man.

-- Preface

Jsonp Is an unofficial protocol that allows the server to integrate SCRIPT tags to return to the client and implement cross-origin access through javascript callback (this is only a simple implementation form of jsonp ). JSON The system development method is a typical data structure-oriented analysis and design method. It is activity-centered and a series of activities are combined into a complete working process.

The reason whyCross-OriginThe root cause of this problem is the browser's same-origin policy restrictions. To understand the same-origin policy restrictions, the same-origin policy is to blockCodeObtain or modify the files or information obtained from another domain name. That is to say, our request address must be the same as that of the current website. The same-origin policy protects resources by isolation. This strategy has a long history since the Netscape Navigator 2.0 era.

A relatively simple solution to this limitation is to send requests on the server side, and the server acts as a proxy relay to reach third-party resources. Although widely used, this method is not flexible enough.

Another method is to use the framework (frames) to include resources of third-party sites, but the resources included are also subject to the same-source policy restrictions.

One clever way is to use dynamic code elements on the page. The source of the code points to the service address and loads data in your own code. When the code is loaded and executed, the same-source policy will not limit the execution. However, if the Code fails to download an object, we can use JSON (JavaScript Object Notation) to improve this application.

JSON and jsonp

JSON is a lightweight data exchange format compared with XML. JSON is attractive to JavaScript developers because JSON itself is an object in Javascript.

For example, a ticker object

VaR ticker = {Symbol: 'ibm ', price: 100}

The JSON string is {Symbol: 'ibm ', price: 100}

In this way, JSON data can be transmitted in the function parameters. We can easily grasp the use of dynamic JSON parameter data in functions, but our goal is not this.

By enabling our functions to load dynamic JSON data, we can process dynamic data. This technology is called dynamic JavaScript insertion.

As mentioned earlier, the same-origin policy is not applicable to dynamically inserted code. That is, you can load code from different domains to execute JSON data in their code.

This is jsonp (JSON with padding ). Note: When using this method, you must define the callback function on the page, just like showprice in the previous example.

The jsonp Service (Remote JSON service) is actually an extended capability to include returned data in user-defined functions. This method relies on accepting the name of a callback function as a parameter.

Then execute this function to process JSON data and display it on the customer page.

Reprinting ended -- From: coblogs

 

I used the $. Ajax function but couldn't get the obtained JSON data. I finally found out the JSON cross-origin problem and found a way to get the JSON data across domains.

 
$.Getjson(URL + "& callbak =? ",Function(Data ){Alert(JSON.Stringify(Data ));}); 

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.