Recently during the development process, ajax was used to asynchronously retrieve images. There is no problem with this function during development, and it can be tested later. However, problems may arise after redeployment. This is the cross-domain problem of Ajax.
ajax itself does not support cross-domain. This is due to the same-origin policy of javascript. But we can solve the cross-domain problem of ajax through other methods.
1 Since we use jquery to write ajax, we initially planned to use jsonp to solve it. The client is written like the following
$.ajax({ type : "get", async:false, url : "http://www.xxx.com/ajax.do", dataType : "jsonp", jsonp: "callbackparam",//服务端用于接收callback调用的function名的参数 jsonpCallback:"success_jsonpCallback",//callback的function名称 success : 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 and has little change from what we wrote before.
2 Since we are developing a lot of pages in this project, the changes involve more places. Finally, it was implemented by directly modifying the nginx configuration. My usual understanding of reverse proxy is caching, security, and load balancing, so I checked the direction proxy below
Reverse Proxy, as the name suggests, is the reverse function of the proxy. We use proxies to access some networks that we cannot directly access, or to hide our true identity. The reverse proxy allows external users to access our internal services behind the firewall without exposing the internal server.
Using a reverse proxy has the following main benefits:
1 Unified control of requests, including setting permissions, filtering rules, etc.;
2 Hide the real address of the internal service, and only the reverse proxy server address is exposed;
3 To achieve load balancing, multiple servers can be used internally to form a server cluster, and one address can still be used for external access;
4 Solve the Ajax cross-domain problem.
5 As a buffer for the real server, it solves the problem of large instantaneous load.
After the project was completed, I checked the ajax cross-domain issue on the Internet, and I also learned that cross-domain purposes can be achieved by requesting tag references for cross-domain resources in HTML. In fact, jsonp essentially uses this A way.
There are many tags in HTML that can request cross-domain resources,
Script is undoubtedly the most suitable. When requesting each script resource, the browser will parse and run the functions 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 parse and execute it in the browser, thereby achieving cross- The purpose of the domain request. Use script tags to implement cross-domain requests, and you can only use the get method to request server resources. And the length of the parameters passed is also limited by the length of the address bar.
From your problem description, I cannot tell what your real problem is. I would like to ask: 1. Does your IIS configuration support anonymous access? 2. Do you use a relative path for the url during ajax interaction? Or is it not the URL of your own website that you are interacting with? Regarding JS cross-domain access, if you must access the resources of other websites, the solutions are: 1. Connect to other websites through iframe 2. Ajax indirectly accesses the resources of the other website through the background, such as accessing the webservice of the other website
参考1
主页面代码:
getDocument2
getElement2