---server-side proxy implementation of Ajax cross-domain

Source: Internet
Author: User

This paper introduces a way to realize cross-domain by JS: Through server-side proxy.

The specific idea: because the browser has the same origin policy limit , (homologous strategy namely: Https://developer.mozilla.org/zh-CN/docs/Web/Security/Same-origin_policy), Therefore, to cross-domain access to resources under other domains, you need to bypass this restriction of the browser, you can set up a proxy on the server side, the server side of the cross-domain site to make requests, and then return the request results to the front end, to successfully avoid the same-origin policy restrictions.

Here's how:

1. In localhost:81/a.html, make a request to an agent under homologous

$.ajax ({    URL: '/proxy.php?name=hello&info=information ',   //server-side agent    type: ' GET ',    success: function () {}})

2, in the agent proxy.php, to the non-homologous server issued a request, obtain the results of the request, the results are returned to the front end.

<?php $name =$_get[' name ']; $info = $_get[' info ']; $crossUrl = ' http://b.com/sub?name= '. $name;   Make a request to another domain $res = file_get_contents ($CROSSURL); echo $res;  ? >

---server-side proxy implementation of Ajax cross-domain

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.