Home Web Front-end JS Tutorial Summary of several methods to achieve cross-domain js (picture ping, JSONP and CORS)_javascript skills

Summary of several methods to achieve cross-domain js (picture ping, JSONP and CORS)_javascript skills

May 16, 2016 pm 03:35 PM
cors jsonp

Cross domain

Although there is a same-origin policy, cross-domain is still very common in js, including document.domain, window.name, image ping, jsonp, and CORS. Here is a brief summary of image ping, jsonp, and CORS preparations. forget.

Picture ping

Images can be loaded from any URL, so setting the src of img to the URL of another domain can achieve simple cross-domain. You can use the onload and onerror events to determine whether a response has been received.

var img=new Image();
img.src='http://www.jb51.net';
img.onerror=function(){
 alert('error');
}
img.onload=function(){
 alert('success');
}
Copy after login

A new img object is created here. The URL given is the blog address. This is an error event, so an error pops up; if the URL is changed to an image http://images.jb51.net//710118 /o_MacBook Air.png, the onload loading information success will pop up, thus achieving simple cross-domain.

Using image ping across domains, you can only send get requests, and you cannot access the text of the response. You can only monitor whether there is a response, which can be used to track ad clicks.

jsonp

jsonp is json with callback function callback. Its original name is json with padding. It is translated as padded json and parameter json.

Because the src of the script can cross domains, add a callback parameter after the sent URL and pass it to the server. Then the data returned by the server will be used as the parameter of the callback. Because this callback is implemented by ourselves, it can be received json data for processing.

The simple code is as follows:

<script type="text/javascript">
function call(data){
 alert(data.city);
}
</script>
<script type="text/javascript" src='http://freegeoip.net/json/&#63;callback=call'></script>
Copy after login

Here, we set the src of the script to http://freegeoip.net/json/?callback=call, which is an API for obtaining the user’s IP address (if interested, you can click here to view ), and then splice callback as a parameter in the URL, and the returned json data will be used as a parameter of the callback. Here we define callback as a call function, that is, the returned json data will be passed in as a parameter of the call. This call function Only the user's city pops up. The output result here is Hebei. For other IP information, you can check the official website, which has a detailed list, such as country_name, time_zone, etc.

CORS (Cross Resource Sharing)

CORS is cross-site resource sharing. It is roughly the same as ajax. For IE, the xdr object, XDomainRequest, is instantiated. The only thing we can access is responseText. The triggered events are load and error. The writing method is roughly the same as xhr, and open and send are also required.

For other browsers such as ff and chrome, xhr is instantiated. Here myvin only uses xhr to demonstrate. If you want to achieve cross-browser, you can cooperate with xdr to achieve compatibility.

xhr is as follows:

var xhr=new XMLHttpRequest();   
var url="http://www.jb51.net";
xhr.open('GET', url); 
xhr.send(null);
Copy after login

The url used here is http://www.jb51.net. The only difference from ajax is that the url uses a cross-domain absolute address. The relative address within this page used in ajax address or absolute address.

Look at the console, ff40.0.3 is used here, and the displayed information is as follows:

Cross-origin request blocked: The same-origin policy prohibits reading the remote resource at http://www.jb51.net. (Cause: CORS header 'Access-Control-Allow-Origin' is missing).

So there is one more step to implement cross-domain using CORS, which is to set Access-Control-Allow-Origin on the server side.

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 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)

How to use CORS cross-domain request in PHP-Slim framework? How to use CORS cross-domain request in PHP-Slim framework? Jun 03, 2023 am 08:10 AM

In web development, cross-domain requests are a common problem. This is because browsers have strict restrictions on requests between different domain names. For example, website A's front-end code cannot send requests directly to website B's API unless website B allows cross-domain requests. In order to solve this problem, CORS (Cross-Origin Resource Sharing) technology emerged. This article will introduce how to use CORS cross-domain requests in the PHP-Slim framework. 1. What is CORSCORS? It is a mechanism that adds some amounts to the corresponding HTTP header.

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 use JSONP to implement cross-domain requests in Vue How to use JSONP to implement cross-domain requests in Vue Oct 15, 2023 pm 03:52 PM

Introduction to how to use JSONP to implement cross-domain requests in Vue. Due to the restrictions of the same-origin policy, the front-end will be hindered to a certain extent when making cross-domain requests. JSONP (JSONwithPadding) is a cross-domain request method. It uses the characteristics of the &lt;script&gt; tag to implement cross-domain requests by dynamically creating the &lt;script&gt; tag, and passes the response data back as a parameter of the callback function. This article will introduce in detail how to use JSONP in Vue

How to build a RESTful API and implement CORS using Golang? How to build a RESTful API and implement CORS using Golang? Jun 02, 2024 pm 05:52 PM

Create a RESTful API and implement CORS: Create the project and install dependencies. Set up HTTP routing to handle requests. Enable cross-origin resource sharing (CORS) using the middlewareCORS middleware. Apply CORS middleware to the router to allow GET and OPTIONS requests from any domain.

Use CORS in Beego framework to solve cross-domain problems Use CORS in Beego framework to solve cross-domain problems Jun 04, 2023 pm 07:40 PM

With the development of web applications and the globalization of the Internet, more and more applications need to make cross-domain requests. Cross-domain requests are a common problem for front-end developers, and it can cause applications to not work properly. In this case, one of the best ways to solve the problem of cross-origin requests is to use CORS. In this article, we will focus on how to use CORS in the Beego framework to solve cross-domain problems. What is a cross-domain request? In web applications, cross-domain requests refer to requests from a web page of one domain name to another

What are the ways springboot solves CORS cross-domain issues? What are the ways springboot solves CORS cross-domain issues? May 13, 2023 pm 04:55 PM

1. Implement the WebMvcConfigurer interface @ConfigurationpublicclassWebConfigimplementsWebMvcConfigurer{/***Add cross-domain support*/@OverridepublicvoidaddCorsMappings(CorsRegistryregistry){//The path that allows cross-domain access '/**' represents all methods of the application registry.addMapping("/** ")//Sources that allow cross-domain access'*

Cross-domain access configuration and CORS protocol support guide for setting up Nginx server Cross-domain access configuration and CORS protocol support guide for setting up Nginx server Aug 04, 2023 pm 10:57 PM

Cross-domain access configuration and CORS protocol support guide for building Nginx servers Introduction: In current web application development, cross-domain requests have become a common requirement. To ensure security, browsers restrict cross-domain operations through AJAX requests by default. The CORS (Cross-Origin Resource Sharing) protocol provides developers with a reliable solution to achieve controllable authorization of cross-domain access. Nginx is a high-performance web server and reverse proxy server. This article will introduce how to use Nginx to build

Why doesn't my Go program use CORS middleware correctly? Why doesn't my Go program use CORS middleware correctly? Jun 10, 2023 pm 01:54 PM

In today's Internet applications, Cross-Origin Resource Sharing (CORS) is a commonly used technology that allows websites to access resources from different domains. During the development process, we often encounter some problems, especially when using CORS middleware. This article will explore why your Go program is not using CORS middleware correctly and provide solutions to these problems. Confirm that the CORS middleware is enabled First, make sure that the CORS middleware is enabled in your Go program. If not enabled, your program will not be able to

See all articles