Home Web Front-end JS Tutorial 4 solutions to Javascript cross-domain requests_javascript skills

4 solutions to Javascript cross-domain requests_javascript skills

May 16, 2016 pm 05:40 PM
ask Cross domain

Under what circumstances does cross-domain occur?
Assume the domain name is: http://www.example.com.cn/
If the requested domain name is different from this domain name, this situation is cross-domain. Since there are loopholes in cross-domain, it is generally normal. The cross-domain request method cannot be requested.
Solution:
1. window.name
1. The server returns
Copy code The code is as follows:

<script>window.name='{"id":"3", "name":"leisure"}';&lt ;/script> <br>2. Define an iframe and add onload event<iframe id="iframe1" onload="iLoad"><iframe> <br><script type="text/javascript"> <br>var load = false; <br>function iLoad() { <br>if(load == false) { <br>// Same domain processing, the iframe will be reloaded again after the request <br>document.getElementById( 'iframe1').contentWindow.location = '/'; <br>load = true; <br>} else { <br>// Get the content of window.name. Note that it must be accessed after the same domain processing! <br>var data = document.getElementById('iframe1').contentWindow.name; <br>alert(data); // {"id":"3", "name":"leisure"} <br>load = false; <br>} <br>} <br></script>

3. Define a form, set the form's target to the id of the iframe, and then submit the form
Copy code The code is as follows:



2. JSONP
The server returns callback({"id": "3", "name": "leisure"});
Copy code The code is as follows:



3. jQuery.getJSON
The server returns json format data test({"id": "3", "name": "leisure"}); test function The name is defined in the callback parameter
Copy code The code is as follows:

$.getJSON(url " ?callback=?", data, function(data) {
}

Note that the callback=? parameter must be brought, jquery will automatically generate a function name to replace the question mark! jQuery.getJSON It is actually implemented using JSONP.
4. Flash cross-domain
Add crossdomain.xml to the server
http://www.example.com.cn/crossdomain.xml
Copy code The code is as follows:




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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Solution to PHP Session cross-domain problem Solution to PHP Session cross-domain problem Oct 12, 2023 pm 03:00 PM

Solution to the cross-domain problem of PHPSession In the development of front-end and back-end separation, cross-domain requests have become the norm. When dealing with cross-domain issues, we usually involve the use and management of sessions. However, due to browser origin policy restrictions, sessions cannot be shared by default across domains. In order to solve this problem, we need to use some techniques and methods to achieve cross-domain sharing of sessions. 1. The most common use of cookies to share sessions across domains

How to make cross-domain requests in Vue? How to make cross-domain requests in Vue? Jun 10, 2023 pm 10:30 PM

Vue is a popular JavaScript framework for building modern web applications. When developing applications using Vue, you often need to interact with different APIs, which are often located on different servers. Due to cross-domain security policy restrictions, when a Vue application is running on one domain name, it cannot communicate directly with the API on another domain name. This article will introduce several methods for making cross-domain requests in Vue. 1. Use a proxy A common cross-domain solution is to use a proxy

How to use Flask-CORS to achieve cross-domain resource sharing How to use Flask-CORS to achieve cross-domain resource sharing Aug 02, 2023 pm 02:03 PM

How to use Flask-CORS to achieve cross-domain resource sharing Introduction: In network application development, cross-domain resource sharing (CrossOriginResourceSharing, referred to as CORS) is a mechanism that allows the server to share resources with specified sources or domain names. Using CORS, we can flexibly control data transmission between different domains and achieve safe and reliable cross-domain access. In this article, we will introduce how to use the Flask-CORS extension library to implement CORS functionality.

How to allow cross-domain use of images and canvas in HTML? How to allow cross-domain use of images and canvas in HTML? Aug 30, 2023 pm 04:25 PM

To allow images and canvases to be used across domains, the server must include the appropriate CORS (Cross-Origin Resource Sharing) headers in its HTTP response. These headers can be set to allow specific sources or methods, or to allow any source to access the resource. HTMLCanvasAnHTML5CanvasisarectangularareaonawebpagethatiscontrolledbyJavaScriptcode.Anythingcanbedrawnonthecanvas,includingimages,shapes,text,andanimations.Thecanvasisagre

How Nginx implements request rewrite configuration based on request URL How Nginx implements request rewrite configuration based on request URL Nov 08, 2023 pm 04:15 PM

Nginx is a lightweight, high-performance web server that not only supports advanced functions such as reverse proxy and load balancing, but also has powerful request rewriting capabilities. In actual web applications, in many cases the request URL needs to be rewritten to achieve better user experience and search engine optimization effects. This article will introduce how Nginx implements request rewriting configuration based on the request URL, including specific code examples. Rewrite syntax In Nginx, you can use the rewrite directive to perform request rewriting. its basic language

Common application scenarios of the Head request method in Laravel Common application scenarios of the Head request method in Laravel Mar 06, 2024 pm 09:33 PM

Common application scenarios of the Head request method in Laravel In Laravel, the HEAD method in the HTTP request method is usually used to obtain the metadata of the resource without obtaining the actual content. The HEAD request is similar to the GET request, but does not return the actual response body content, only the response header information. This makes the HEAD request very useful in some specific scenarios. The following are some common application scenarios and corresponding code examples. Verify the validity of the link using the HEAD request method can be used to verify the chain

Cross-domain problems encountered in Vue technology development and their solutions Cross-domain problems encountered in Vue technology development and their solutions Oct 08, 2023 pm 09:36 PM

Cross-domain problems and solutions encountered in the development of Vue technology Summary: This article will introduce the cross-domain problems and solutions that may be encountered during the development of Vue technology. We'll start with what causes cross-origin, then cover a few common solutions and provide specific code examples. 1. Causes of cross-domain problems In web development, due to the browser's security policy, the browser will restrict requests from one source (domain, protocol or port) for resources from another source. This is the so-called "same origin policy". When we are developing Vue technology, the front-end and

How to use context to implement request retry strategy in Go How to use context to implement request retry strategy in Go Jul 21, 2023 pm 04:39 PM

How to use context to implement request retry strategy in Go Introduction: When building a distributed system, network requests will inevitably encounter some failures. In order to ensure the reliability and stability of the system, we usually use a retry strategy to handle these failed requests to increase the success rate of the request. In the Go language, we can use the context package to implement the request retry strategy. This article will introduce how to use the context package in Go to implement a request retry strategy, with code examples. 1. What is

See all articles