Home  >  Article  >  Web Front-end  >  Summary of using json and jsonp

Summary of using json and jsonp

php中世界最好的语言
php中世界最好的语言Original
2018-04-24 17:21:301208browse

This time I will bring you a summary of the use of json and jsonp. What are the precautions when using json and jsonp. The following is a practical case, let's take a look.

json

1. The value of json can be of the following types:

① Number (integer or floating point number) , such as 123, 1.23

String (in double quotes)

③ Logical value (true or false)

④ Array (in In square brackets)

⑤ Object (in curly brackets)

⑥ null

2. json parsing method

① eval('(' jsondata ')' ); is always unsafe to use, code injection

② JSON.parse(jsondata);

JSONLint json: String verification tool

3. jQuery implements ajax

jQuery.ajax([settings])

type: type

url: to send the request Address

data: is an object, along with the data sent to the server in the request

dataType: the data type expected to be returned by the server, if not specified, jQuery will automatically use HTTP Include MIME information for intelligent judgment. Generally, we use json format, which can be set to "json"

success: It is a method, the callback function after the request is successful, pass in the returned data, and a string containing the success code

error: is a method that is called when the request fails. Pass in the XMLHttpRequest object

jsonp

Cross-domain

The composition of a domain name address:

http://www .abc.com :8080 / scripts/jquery.js

Protocol://subdomain name. Main domain name: port number/requested resource address

When any one of the protocol, subdomain name, main domain name, and port number is not available at the same time, it is counted as a different domain

Different domains request resources from each other, and it is counted as "cross-domain"

javascript For security reasons, cross-domain calls to objects on other pages are not allowed.

What is cross-domain? The simple explanation is that due to the restrictions of the JavaScript same-origin policy, js under the domain name a.com cannot operate objects under the domain name b.com or c.a.com

Cross-domain processing method:

—— Proxy (belonging to the background technology), for example, calls the Shanghai server service in the background of the web server in Beijing, and then returns the response result to the front end, so that the front end calls the server with the same domain name in Beijing and calls Shanghai The service effect is the same.

——JSONP

a domain name is used to declare, b domain name is used to call

Note: JSONP can only be used for GET requests, and does not support POST requests (Limitations)

——

## Just make some small modifications on the server side:

header("Access-Control-Allow-Origin:"); means that all servers can access it, or it can be replaced with a specific Domain name, for example: the server is in Shanghai, * change to the domain name of the Beijing server, so that only the Beijing domain name can be accessed

header("Access-Control-Allow-Methods:POST,GET");

Let’s talk about the advantages and disadvantages of jsonp.

Same-origin policy: JavaScript can only access content in the same domain as the document containing it. jsonp can span the same origin policy. When we use jsonp, it will be another protocol for communication.

The advantage of JSONP is that it is not restricted by the same origin policy like the Ajax request implemented by the XMLHttpRequest object; it has better compatibility and can run in older browsers without the support of XMLHttpRequest or ActiveX ; And after the request is completed, the result can be returned by calling callback.

The disadvantages of JSONP are: it only supports GET requests and does not support other types of HTTP requests such as POST; it only supports cross-domain HTTP requests and cannot solve the problem between two pages in different domains. Issues making JavaScript calls.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Adjustment of ajax execution order in jquery

Detailed explanation of Ajax and $.ajax instances

How jQuery makes ajax request for monitoring page

The above is the detailed content of Summary of using json and jsonp. For more information, please follow other related articles on the PHP Chinese website!

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