Ajax cross-domain solution, Ajax cross-domain _php tutorial

Source: Internet
Author: User

Ajax cross-domain solution, Ajax cross-domain


Recently, in the development process, Ajax was used to extract images asynchronously. In the development of this feature is not a problem, you can later to test, re-deployment after the problem, this is the cross-domain Ajax problem.

Ajax itself does not support cross-domain, which is caused by the same- origin policy of JavaScript. But there are other ways to solve the cross-domain problem of Ajax.

1 since we are using jquery to write Ajax, we are initially prepared to use JSONP to solve, the client is similar to the following wording

$.ajax ({type: "Get", Async:false,url: "Http://www.xxx.com/ajax.do", DataType: "Jsonp", Jsonp: "Callbackparam",// The server is used to receive parameter Jsonpcallback for the function name of the callback call: "Success_jsonpcallback",//callback's function names Success:function (JSON {alert (JSON); alert (json[0].name);},error:function () {alert (' fail ');}});

Server-side notation

public void ProcessRequest (HttpContext context) {context. Response.ContentType = "Text/plain"; String Callbackfunname = context. request["Callbackparam"];context. Response.Write (Callbackfunname + "([{name:\" john\ "}])");

This method is actually quite simple, not as much as the changes we wrote earlier.

2 because of the more pages we developed this time, there are more places to change them. Finally, the implementation of the directly modified Nginx configuration is taken. At ordinary times the understanding of the reverse proxy is the cache, security, load balancing, so check the bottom to the agent

Reverse proxy (Reverse proxy), as the name implies, is the reverse function of the agent. We use proxies to access some of the networks that we don't have direct access to, or to hide our true identities. The reverse proxy allows external users to access our internal and firewall services without exposing the internal servers.

Using a reverse proxy mainly has the following benefits:

1 Unified control of the request, including setting permissions, filtering rules, etc.;

2 Hide the internal service real address, exposed only the reverse proxy server address;

3 to achieve load balancing, the internal can use multiple servers to form a server cluster, external or can take an address access;

4 Troubleshoot Ajax cross-domain issues.

5 as the buffer of the real server, solve the problem of the large amount of instantaneous load.

After the project is completed, the Ajax cross-domain problem is checked on the internet, but also know that through the HTML can request cross-domain resources of the label reference to achieve the purpose of cross-domain, in fact, JSONP is essentially using this approach.

There are many tags in the HTML that can request cross-domain resources.

The script is undoubtedly the most appropriate. When each script resource is requested, the browser parses and runs the function defined in the script file, or the JavaScript code that needs to be executed immediately, we can return a script or JSON object through the server, and execute in the browser to achieve the purpose of cross-domain request. Using the script tag to implement cross-domain requests, you can only request server resources by using the Get method. And the length of the parameter is also limited by the length of the address bar.


Ajax cross-domain access workarounds

From your question description, I can't see what your real problem is, excuse me: First, do you support anonymous access in your IIS configuration? Second, do you use the relative path for the URL when you interact with Ajax? Or do you interact with the URLs of your own sites? About JS cross-domain access, if you do not want to access the resources of other sites, the solution is: first, through the IFRAME connection to other sites second, Ajax indirectly through the backstage access to the site's resources, such as access to the other site webservice

For Ajax cross-domain solutions

Reference 1

Main Page code:



<title>Main Page </title></title>

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.