How to solve the problem of cross-domain js

How to solve the problem of cross-domain js
Js cross-domain issue is one of the issues most commonly encountered web developer. Js so-called cross-domain problem, refer to the page in a domain data access objects under a different domain by js, for safety reasons, almost all browsers do not allow such cross-domain access, which resulted in Some ajax applications using cross-domain web service can be a problem. Js solve cross-domain problems, currently has a number of ready-made solution for the client and server, but these programs can not solve all the problems. Let us first look at what common solution, and gives a space its own solution for space demand for cross-domain issues, hoping to have reference to other product groups.

Client solution
how to solve the problem of cross-domain js in the client almost all web developers will be the first consideration. The most commonly used methods are two kinds: Set document.domain, loaded by the script tag.

Document.domain provided
using this method is provided according to two cross-domain requests a page must belong to a domain basis (e.g. all xxx.com, or xxx.com.cn), using the same protocol (e.g. all http) and the same port (eg both 80). For example, aaa.xxx.com inside a page bbb.xxx.com need to call in an object, then document.domain two pages are set to xxx.com, we can achieve cross-domain calls. Also, note that in this way can only be used in the Father, the Son page that is useful only when the data access performed with iframe.

By loading script tag
for browsers, the script tag src attribute of the resource to the resource just like the img tag src attribute points to the same, is a static resource, the browser will automatically go to load these resources at the appropriate time, the so-called cross-domain problem does not arise. Data objects so that we can attribute to be accessed by the reference into the current page, bypassing js cross-domain issues. For example, in my space project space, the need for random recommended in the management center under hi-domain pages of several popular modules to the user, because the relevant information on popular modules are maintained in php module under the act domain, if directly in the hi the domain to acquire the recommendation module under the act by domain ajax request list information appeared js cross-domain problems. The easiest way to solve this problem is, in the hi-domain to access the http interface to act domains provided by the script tag:

Guess you like

Origin www.cnblogs.com/dillonmei/p/12578207.html