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/*** the interface forwarding layer in Ajax business processing solves the problem of Ajax cross-origin access * Working principle: Ask the request to be transited through this program, interaction with remote service interfaces on the local server layer * Note: during use, the url_root parameter needs to be modified according to your target interface address, this forwarding layer can be used by a single interface Web Service Interface Service * program to support 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 (where modification is required) */const url_root = 'HTTP: // api.air-id.net/interface/'{/####/const charset = 'utf-8';/** get */PRI Vate $ msgets = '';/** post */private $ magetpostdata = array (); function _ construct () {$ this-> getpost (); $ this-> getget (); if ($ this-> msgets! = ''| Count ($ this-> magetpostdata)> 0) {// 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 () {/* Get get content */If (count ($ _ Get)> 0) {$ atmp = array (); foreach ($ _ get as $ skey => $ sval) $ atmp [] = $ skey. '= '. urlencode ($ sval); $ this-> msgets = implode ('&', $ Atmp); Return true;} elsereturn false;}/*** read the content returned by the remote interface * @ return string **/private function getcontent ($ sgeturl) {/**/$ CH = curl_init (); curl_setopt ($ ch, curlopt_url, $ sgeturl); // set the get URL curl_setopt ($ ch, curlopt_returntransfer, true ); // Save the result as a string curl_setopt ($ ch, curlopt_connecttimeout, 10); // connection timeout scurl_setopt ($ ch, curlopt_timeout, 10 ); // execution timeout scurl_setopt ($ ch, curlopt_dns_cache_timeout, 18 00); // DNS resolution cache retention time: curl_setopt ($ ch, curlopt_header, 0); // If (count ($ this-> magetpostdata)> 0) {// curl_setopt ($ ch, curlopt_post, 1) must be submitted for post data. // enable the 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.