Home  >  Article  >  Web Front-end  >  JSONP cross-domain GET request solves Ajax cross-domain access problem

JSONP cross-domain GET request solves Ajax cross-domain access problem

PHPz
PHPzOriginal
2016-05-16 16:23:102049browse

This article mainly introduces how JSONP cross-domain is implemented, and discusses the principle of JSONP cross-domain. As well as using JSONP cross-domain GET requests to solve Ajax cross-domain access problems, friends who need it can refer to

A few days ago, there was a new requirement at work, which required the front-end web page to asynchronously call the background Webservice method to return information. There are many implementation methods. This example uses jQuery Ajax. After completion, everything was ok when debugging locally. However, problems occurred after deploying to the server. The background service call did not respond. What happened? The code has not changed much. The only modification is the url address in jQuery's ajax method. Is this the problem here? After inspection and debugging, I found that it was the same origin policy that was causing trouble. We know that JavaScript or jQuery is a dynamic scripting technology often used in Web front-end development. In JavaScript, there is a very important security restriction called "Same-Origin Policy". This policy places important restrictions on the page content that JavaScript code can access, that is, JavaScript can only access content under the same domain name as the document or script that contains it. Scripts under different domain names cannot access each other, even in subdomains. Regarding the same-origin policy, readers can find a more detailed explanation on Baidu and will not go into details here.

But sometimes cross-domain operations are unavoidable. At this time, the "same origin policy" is a limitation. What should I do? Using JSONP cross-domain GET request is a common solution. Let's take a look at how JSONP cross-domain is implemented and explore the principle of JSONP cross-domain.

JSONP is mentioned here, so someone asked, what are the differences and differences between it and JSON? Let’s take a look. Baidu Encyclopedia has the following explanation:

JSON( JavaScript Object Notation) is a lightweight data exchange format. It is based on a subset of JavaScript (Standard ECMA-262 3rd Edition - December 1999). JSON uses a completely language-independent text format, but also uses conventions similar to the C language family (including C, C, C#, Java, JavaScript, Perl, Python, etc.). These properties make JSON an ideal data exchange language. It is easy for people to read and write, and it is also easy for machines to parse and generate (network transmission speed is fast).

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn