Ajax cross-origin solution: ajax cross-origin _ PHP Tutorial

Source: Internet
Author: User
Ajax cross-origin solution: ajax cross-origin. Ajax cross-origin solution: recently, in the development process, ajax is used to asynchronously retrieve images. There is no problem with this function during development. you can test it later. After redeployment, the ajax cross-origin solution will be available.

Recently, in the development process, ajax is used to asynchronously retrieve images. There is no problem with this function during development. you can test it later and there will be a problem after redeployment. this is the cross-origin issue of ajax.

Ajax itself does not support cross-origin, because javascriptSame-origin policy. However, we can use other methods to solve the cross-origin issue of ajax.

1. because we use jquery to write ajax, we are prepared to use jsonp at the beginning. the client is similar to the following method.

$. Ajax ({type: "get", async: false, url: "http://www.xxx.com/ajax.do", dataType: "jsonp", jsonp: "callbackparam ", // The parameter jsonpCallback of the function name used by the server to receive callback calls: "success_jsonpCallback", // the function name success of callback: function (json) {alert (json ); alert (json [0]. name) ;}, error: function () {alert ('fail ');}});

Server-side writing

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, but it is not much changed as we have previously written.

2. due to the large number of pages we developed in this project, the changes involved a lot. The last step is to directly modify the nginx configuration. I usually understand the reverse proxy, that is, cache, security, and load balancing. so I checked the downstream proxy.

Reverse Proxy, as its name implies, is the Reverse Proxy function. We use a proxy to access networks that we cannot directly access, or to hide our real identities. Reverse proxy allows external users to access our internal and firewall services without exposing internal servers.

Reverse proxy has the following benefits:

1. Unified request control, including permission setting and filtering rules;

2. hide the actual internal service address. only the reverse proxy server address is exposed;

3. implement load balancing. multiple internal servers can be used to form a server cluster, and an external address can be used for access;

4. Solve the cross-origin issue of Ajax.

5. as a buffer for real servers, this solution solves the problem of high load instantly.

After the project is completed, I checked the ajax cross-origin issue on the internet and found that the cross-origin resource tag reference can be requested in HTML to achieve the cross-origin goal, in fact, jsonp basically adopts this method.

There are many tags in HTML that can request cross-origin resources,

Script is undoubtedly the most appropriate. When requesting every script resource, the browser will parse and run the functions defined in the script file, or JavaScript code that needs to be executed immediately. we can return a script or JSON object through the server, execute the parsing in the browser to achieve the purpose of cross-origin requests. Use the script tag to implement cross-origin requests. you can only use the get method to request server resources. The length of parameter passing is also limited by the address bar length.


AJAX cross-origin access solution

I cannot tell from your problem description what is your real problem. May I ask: 1. does your IIS configuration support anonymous access? 2. do you use relative paths for URLs during ajax interaction? Or is the interaction not the website address? For cross-origin access to JS, if you have to access resources of other websites, the solution is as follows: 1. connect to other websites through iframe. 2. ajax indirectly accesses resources of the other website through the background, for example, to access the webservice of the website
 

Cross-Origin ajax solution

Reference 1

Homepage code:



Home page </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.