To solve cross-origin ajax problems, the following methods are provided on the Internet:
1 is to build a server-side proxy. In short, it means that the essence of ajax calls is the local url, and the server side retrieves the remote address for js.
2. Generate a tag using the script tag. After loading js, perform subsequent operations.
That is, the new xmlHTTPrequest object operation is changed to the new script tag operation.
Here is an example:
# Ajah. js
Var Ajah = function (url, varname, handleSuccess, handleFailure ){
/**
* HandleSuccess, handleFailure must be functions
**/
Script = document. createElement ("script ");
Script. src = url;
Var handler = function (str)
{
HandleSuccess (str );
}
Script. onload = function ()
{
Var json = eval (varname );
Handler (json );
}
If (window. ie)
{
Script. onreadystatechange = function ()
{
If (script. readyState = 'complete' | script. readyState = 'loaded ')
{
Var json = eval (varname );
If (typeof json! = 'Undefined ')
{
Handler (json );
}
}
}
}
Document. body. appendChild (script );
}
In the web page, you should call the following method:
Includemo.html
<Pre>
<! DOCTYPE html PUBLIC "-// W3C // dtd html 3.2 // EN">
<Html>
<Head>
<Meta name = "generator" content =
"HTML Tidy for Linux (vers 1 September 2005), see www.w3.org">
<Title> </title>
<Script src = "mootools. js"> </script>
<Script src = "ajah. js"> </script>
</Head>
<Body>
<Script>
Var ajah = new Ajah ("data. js", "json198", function (str ){
Console. debug ("returned :");
Console. debug (str );
},
Function (str ){});
</Script>
</Body>
</Html>
</Pre>
The called data file is as follows:
# Data. js
Var json198 = "hello, this is json198 ";
Funciton Ajah (url, varname, handleSuccess, handleFailure ){...}
The Ajah constructor calls four parameters:
Url: Remote address
Varname: variable name of the remote returned data
HandleSuccess: function loaded after loading
HandleFailure: Temporarily unavailable