AJAX cross-origin request-JSONP usage, PHP instance details

Source: Internet
Author: User
Today, I added a verification code to a project. I have a server with a complete set of verification and verification code generation logic. I wanted to directly use ajax to access the server for verification, after trying for a long time, the system failed to return the status code 200, but the data was always blank. After the query, ajax had to have a special expression to support cross-origin requests and the get method was required.

Today, I added a verification code to a project. I have a server with a complete set of verification and verification code generation logic. I wanted to directly use ajax to access the server for verification, after trying for a long time, the system failed to return the status code 200, but the data was always blank. After the query, ajax had to have a special expression to support cross-origin requests and the get method was required.

Today, I added a verification code to a project. I have a server with a complete set of verification and verification code generation logic. I wanted to directly use ajax to access the server for verification, after trying for a long time, the system failed to return the status code 200, but the data was always blank. After the query, ajax needed a special format to support cross-origin requests, and the get method was required. The following is a detailed introduction:

An ideal way to overcome the same-source restriction is to insert a dynamic script element into the Web page. The page source points to the service URL in another domain and obtains data in its own script. When the script is loaded, it starts to execute. This method is feasible because the same-origin policy does not prevent dynamic script insertion and regards the script as being loaded from the domain that provides the Web page. However, if the script tries to load the document from another domain, it will not succeed. Fortunately, this technology can be improved by adding JavaScript Object Notation (JSON.

1. What is JSONP?

To understand JSONP, I have to mention JSON. What is JSON?

JSON is a subset of the object literal notation of JavaScript. Since JSON is a subset of JavaScript, it can be used in the language with no muss or fuss.

JSONP (JSON with Padding) is an unofficial protocol that allows the server to integrate Script tags to return to the client, cross-origin access is implemented through javascript callback (this is only a simple implementation form of JSONP ).

2. What is JSONP used?

3. How to Use JSONP?

What is jsonp format? Original API: If the obtained data file is stored on a remote server (the domain name is different, that is, cross-Origin data retrieval), The jsonp type is required. If this type is used, a query string parameter callback =? will be created? This parameter is added after the request URL. The server should add the callback function name before the JSON data to complete a valid JSONP request. This means that the remote server needs to process the returned data and return a callback (json) data according to the callback parameter submitted by the client, the client will process the returned data in script Mode to process json data. JQuery. getJSON also supports jsonp data calling.

First, let's take a look at my example:

Js Code

Explanation: This is a frequently-seen ajax method of jquery. The jsonp format is used here. Note that one of the parameters jsonp: 'callback ', will get be used for ajax requests? Callback = jsonp1377158771666 & _ = 1377159265083 & code = asd, which is very useful because it will be transmitted to the php page.

The following is the php code:

Note: In the output section, add the callback parameter just transferred. The value of this parameter should be jsonp1377158771666. This parameter must be output in front of the output, and the result will be successful. The result obtained by js is $ r, which is a json file, just like ajax.

Of course, there are many other ways to write. The following are examples of conversion:

HTML code (any ):

Html code

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.