Home Web Front-end JS Tutorial jquery solves client cross-domain access problem_jquery

jquery solves client cross-domain access problem_jquery

May 16, 2016 pm 04:22 PM
jquery client Cross domain

Client-side "cross-domain access" has always been a headache. Fortunately, with the help of jQuery, cross-domain problems have been solved since jQuery-1.2. Since I encountered cross-domain problems in the project, I took this opportunity to inquire into the cross-domain issues, consulted relevant information and my own practice, and finally solved the cross-domain problems. Record it for reference.

jQuery.ajax() supports cross-domain get methods, which is actually done using jsonp.

Real case:

Copy code The code is as follows:

$.ajax({
async:false,
​​​​​​ url: 'http://www.mysite.com/demo.do', // Cross-domain URL
            type: 'GET',
DataType: 'jsonp',
               jsonp: 'jsoncallback', //Default callback
data: mydata,
Timeout: 5000,
                beforeSend: function(){ //This method is not triggered in jsonp mode. The reason may be that if dataType is specified as jsonp, it is no longer an ajax event
            },
Success: Function (JSON) {// Client JQuery's pre -defined Callback function. After successfully obtaining JSON data on cross -domain servers, this callback function
If(json.actionErrors.length!=0){
alert(json.actionErrors);
                                                                                                                           genDynamicContent(qsData,type,json);
             },
complete: function(XMLHttpRequest, textStatus){
                        $.unblockUI({ fadeOut: 10 });
             },
​​​​​​ error: function(xhr){
                                                                                                                                                 out out out out out out wanting out being out out over through out over over being over so so so so so so so so so so so so so so to so so so so so so so so so so so so so so so so so so so to so so so so so so so so to to to to to to to to to to to toto To to to to to to to to to to to to to to to to To To to To to to To to toss with with this method with this method method method method method method to method method to method method not to trigger
//Request error handling
alert("Request error (please check the relevant network status.)");
               }
        });

Note:

Copy code The code is as follows:
$.getJSON("
http://www.mysite.com/demo.do?name1=" value1 "&callback=?",               function(json){
If(json.property name==value){
//Execute code
                                                                                                               });

This method is actually an advanced encapsulation of the $.ajax({..}) api in the above example. Some of the underlying parameters of the $.ajax api are encapsulated and not visible.

On the server side, use callback= request.getParameter("callback") to get the jsonp32440980 to be called back later on the jQuery side
Then return something like: "jsonp32440980(" json array to be returned ")";
jquery will dynamically load and call this through the callback method: jsonp32440980 (json array);
This achieves the purpose of cross-domain data exchange.
The most basic principle of jsonp is: dynamically adding one is consistent (qq space uses this method to achieve cross-domain data exchange). JSONP is a kind of script injection (Script Injection) behavior, so it also has certain security risks.

Note: jquey does not support post cross-domain.
This is because although the use of post to dynamically generate iframes can achieve the purpose of post cross-domain (this is how a js expert patched jquery1.2.5), this is a relatively extreme method and is not recommended. It can also be said that the cross-domain access method of get is legal, and the post method is considered illegal from a security perspective. It is better not to post as a last resort. The demand for cross-domain access on the client side seems to have attracted the attention of w3c. According to the information, html5 The WebSocket standard supports cross-domain data exchange and should be an optional cross-domain data exchange solution in the future.

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

VMware Horizon client freezes or stalls while connecting [Fix] VMware Horizon client freezes or stalls while connecting [Fix] Mar 03, 2024 am 09:37 AM

When connecting to a VDI using the VMWareHorizon client, we may encounter situations where the application freezes during authentication or the connection blocks. This article will explore this issue and provide ways to resolve this situation. When the VMWareHorizon client experiences freezing or connection issues, there are a few things you can do to resolve the issue. Fix VMWareHorizon client freezes or gets stuck while connecting If VMWareHorizon client freezes or fails to connect on Windows 11/10, do the below mentioned solutions: Check network connection Restart Horizon client Check Horizon server status Clear client cache Fix Ho

PHP MQTT Client Development Guide PHP MQTT Client Development Guide Mar 27, 2024 am 09:21 AM

MQTT (MessageQueuingTelemetryTransport) is a lightweight message transmission protocol commonly used for communication between IoT devices. PHP is a commonly used server-side programming language that can be used to develop MQTT clients. This article will introduce how to use PHP to develop an MQTT client and include the following content: Basic concepts of the MQTT protocol Selection and usage examples of the PHPMQTT client library: Using the PHPMQTT client to publish and

Detailed explanation of jQuery reference methods: Quick start guide Detailed explanation of jQuery reference methods: Quick start guide Feb 27, 2024 pm 06:45 PM

Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded

How to solve the problem that the Baidu Netdisk webpage cannot start the client? How to solve the problem that the Baidu Netdisk webpage cannot start the client? Mar 13, 2024 pm 05:00 PM

When many friends download files, they will first browse on the web page and then transfer to the client to download. But sometimes users will encounter the problem that the Baidu Netdisk webpage cannot start the client. In response to this problem, the editor has prepared a solution for you to solve the problem that the Baidu Netdisk webpage cannot start the client. Friends in need can refer to it. Solution: 1. Maybe Baidu Netdisk is not the latest version. Manually open the Baidu Netdisk client, click the settings button in the upper right corner, and then click version upgrade. If there is no update, the following prompt will appear. If there is an update, please follow the prompts to update. 2. The detection service program of Baidu Cloud Disk may be disabled. It is possible that we manually or use security software to automatically disable the detection service program of Baidu Cloud Disk. Please check it out

How to use PUT request method in jQuery? How to use PUT request method in jQuery? Feb 28, 2024 pm 03:12 PM

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

How to remove the height attribute of an element with jQuery? How to remove the height attribute of an element with jQuery? Feb 28, 2024 am 08:39 AM

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

jQuery Tips: Quickly modify the text of all a tags on the page jQuery Tips: Quickly modify the text of all a tags on the page Feb 28, 2024 pm 09:06 PM

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: &lt

Use jQuery to modify the text content of all a tags Use jQuery to modify the text content of all a tags Feb 28, 2024 pm 05:42 PM

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on ​​the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

See all articles