When you create a js ajax application, the interface you need to request is not in your current domain. in this case, the cross-origin access problem occurs, and the browser will forbid you to request this interface. How can I access this WebService interface? A simple method is to add a switch... SyntaxHighlighter. all () on the server of the local domain ();
When you create a js ajax application, the interface you need to request is not in your current domain. in this case, the cross-origin access problem occurs, and the browser will forbid you to request this interface.
How can I access this WebService interface?
A simple method is to add a forwarding layer on the server of the local domain. after receiving the request from the browser, the server forwards the request to the corresponding WebService, then retrieve the returned results and return them to the js request page.
Generally, this is the most secure and compatible method for cross-origin access.
The following is a php script I wrote to complete the forwarding process for your reference only:
[Php]
/**
* The interface forwarding layer in ajax business processing solves the problem of cross-origin ajax access.
* Working principle: requests are transferred through this program, and interaction with remote service interfaces is completed at the local server layer.
* Note: During use, the URL_ROOT parameter needs to be modified based on your target interface address. This forwarding layer can be used for the Web Service interface Service of a single interface.
* The program supports simultaneous forwarding of POST data and GET data;
* @ Version 1.0.0.2
* @ Author JerryLi lijian@dzs.mobi
* @ Copyright B .dzs.mobi 2012-11-16
**/
Class interface_relay
{
/** Interface root address (here is the place to be modified )*/
Const URL_ROOT = 'http: // api.air-id.net/interface /';
/** Character set */
Const CHARSET = 'utf-8 ';
/** GET */
Private $ msGets = '';
/** POST */
Private $ maGetPostData = array ();
Function _ construct ()
{
$ This-> getPOST ();
$ This-> getGET ();
If ($ this-> msGets! = ''| Count ($ this-> maGetPostData)> 0)
{// Input data exists
If (count ($ this-> msGets)> 0)
$ SUrl = self: URL_ROOT .'? '. $ This-> msGets;
Else
$ SUrl = self: URL_ROOT;
Header ('content-Type: text/html; charset = '. self: CHARSET );
Echo $ this-> getContent ($ sUrl );
}
Else
{
Header ('content-Type: text/html; charset = '. self: CHARSET );
Echo $ this-> getContent (self: URL_ROOT );
}
}
Function _ destruct ()
{
Unset ($ maGetPostData, $ msGets );
}
/**
* Load POST data
* @ Return bool
**/
Private function getPOST ()
{
$ Handle = @ fopen ('php: // input', 'r ');
$ Data = '';
Do
{
$ Data = @ fread ($ handle, 1024 );
If (strlen ($ data) = 0)
Break;
Else
$ This-> maGetPostData [] = $ data;
} While (true );
Fclose ($ handle );
Unset ($ data, $ handle );
Return count ($ this-> maGetPostData)> = 1;
}
/**
* Load GET data
* @ Return bool
**/
Private function getGET ()
{Www.2cto.com
/* GET content */
If (count ($ _ GET)> 0)
{
$ ATmp = array ();
Foreach ($ _ GET as $ sKey => $ sVal)
$ ATmp [] = $ sKey. '='. urlencode ($ sVal );
$ This-> msGets = implode ('&', $ aTmp );
Return true;
}
Else
Return false;
}
/**
* Read the content returned by the remote interface
* @ Return string
**/
Private function getContent ($ sGetUrl)
{
/**/
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, $ sGetUrl); // you can specify the get url.
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true); // Save the result as a string
Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, 10); // connection timeout s
Curl_setopt ($ ch, CURLOPT_TIMEOUT, 10); // execution timeout s
Curl_setopt ($ ch, CURLOPT_DNS_CACHE_TIMEOUT, 1800); // The DNS resolution cache is saved for half an hour.
Curl_setopt ($ ch, CURLOPT_HEADER, 0); // discard the header information
If (count ($ this-> maGetPostData)> 0)
{// The POST data must be submitted.
Curl_setopt ($ ch, CURLOPT_POST, 1); // enable POST data
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, implode ('', $ this-> maGetPostData); // submit POST data
}
$ SData = curl_exec ($ ch );
Curl_close ($ ch );
Unset ($ ch );
Return $ sData;
}
}
$ O = new interface_relay ();
Unset ($ o );
?>