Use PHP as the server forwarding layer to solve ajax cross-origin access problems in Javascript

Source: Internet
Author: User

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]
<? 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 );
?>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.