Summary of using json and jsonp
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 accessedheader("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
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



MySQL5.7 and MySQL8.0 are two different MySQL database versions. There are some main differences between them: Performance improvements: MySQL8.0 has some performance improvements compared to MySQL5.7. These include better query optimizers, more efficient query execution plan generation, better indexing algorithms and parallel queries, etc. These improvements can improve query performance and overall system performance. JSON support: MySQL 8.0 introduces native support for JSON data type, including storage, query and indexing of JSON data. This makes processing and manipulating JSON data in MySQL more convenient and efficient. Transaction features: MySQL8.0 introduces some new transaction features, such as atomic

Performance optimization methods for converting PHP arrays to JSON include: using JSON extensions and the json_encode() function; adding the JSON_UNESCAPED_UNICODE option to avoid character escaping; using buffers to improve loop encoding performance; caching JSON encoding results; and considering using a third-party JSON encoding library.

Quick Start: Pandas method of reading JSON files, specific code examples are required Introduction: In the field of data analysis and data science, Pandas is one of the important Python libraries. It provides rich functions and flexible data structures, and can easily process and analyze various data. In practical applications, we often encounter situations where we need to read JSON files. This article will introduce how to use Pandas to read JSON files, and attach specific code examples. 1. Installation of Pandas

Annotations in the Jackson library control JSON serialization and deserialization: Serialization: @JsonIgnore: Ignore the property @JsonProperty: Specify the name @JsonGetter: Use the get method @JsonSetter: Use the set method Deserialization: @JsonIgnoreProperties: Ignore the property @ JsonProperty: Specify name @JsonCreator: Use constructor @JsonDeserialize: Custom logic

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

In-depth understanding of PHP: Implementation method of converting JSONUnicode to Chinese During development, we often encounter situations where we need to process JSON data, and Unicode encoding in JSON will cause us some problems in some scenarios, especially when Unicode needs to be converted When encoding is converted to Chinese characters. In PHP, there are some methods that can help us achieve this conversion process. A common method will be introduced below and specific code examples will be provided. First, let us first understand the Un in JSON

PHP arrays can be converted to JSON strings through the json_encode() function (for example: $json=json_encode($array);), and conversely, the json_decode() function can be used to convert from JSON to arrays ($array=json_decode($json);) . Other tips include avoiding deep conversions, specifying custom options, and using third-party libraries.

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service
