Solution to Ajax Cross-domain problem in JavaScript

Source: Internet
Author: User
Tags json script tag

In the recent development process, using AJAX to asynchronously fetch pictures. In the development of this feature is not a problem, you can later test, redeployment after the problem, this is the problem of Ajax Cross-domain.

Ajax itself does not support cross-domain, as a result of JavaScript's homology strategy. But there are other ways to solve the cross-domain problem of Ajax.

1 because we are using jquery to write Ajax, we are ready to use JSONP to solve, the client is similar to the following wording

The code is as follows Copy Code

$.ajax ({
Type: "Get",
Async:false,
URL: "Http://www.xxx.com/ajax.do",
DataType: "Jsonp",
JSONP: "Callbackparam",//The parameter of the function name used by the server to receive the callback call
Jsonpcallback: "Success_jsonpcallback",//callback's function name
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, with the changes we have written before is not big.

2 because of our project development of more pages, the changes involved in more places. Finally, a direct modification of the Nginx configuration is implemented. At ordinary times the understanding of the reverse proxy is caching, 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 can't access directly, or we can hide our true identities. And the reverse proxy, can not expose the internal server situation, so that external users access to our internal, firewall services.

Using reverse proxies has the following main benefits:

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

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

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

4 Solve the Ajax Cross-domain problem.

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

After the project is completed, the Ajax cross-domain problem is checked on the Internet, and it is known that through the HTML can request the Cross-domain resources of the label reference to achieve the goal of Cross-domain, in fact, JSONP is essentially the use of this approach.

There are a lot of tags in HTML that can request Cross-domain resources,

Script is undoubtedly the most appropriate. When requesting each script resource, the browser parses and runs the functions defined within the script file, or the JavaScript code that needs to be executed immediately, and we can return a script or JSON object from the server and parse the execution in the browser to achieve the goal of Cross-domain request. Using the script tag to implement Cross-domain requests, you can only request server resources using the Get method. The length of the argument is also limited by the length of the address bar.

Add: After jquery, how to solve the cross-domain problem of Ajax:

The code is as follows Copy Code

<title>jquery Learning </title>
<script src= "Jquery-1.4.2.min.js" type= "Text/javascript" ></script>
<script type= "Text/javascript" >
$ (document). Ready (function () {
var obtntest = $ ("#btnTest");
Obtntest.click (function () {
Obtntest.disabled = true;
var Oresult = $ ("#result");
Oresult.html ("Loading"). CSS ("Color", "red");
Jquery.getscript ("Http://www.111cn.net/test/js.txt",
function () {
Oresult.html ("Name:" + Dylan.name + "<br/>email:" + dylan.email). CSS ("Color", "black");
obtntest.disabled = false;
});
});
});
</script>
<body>
<button id= "Btntest" >BtnTest</button>
<div id= "Result" ></div>
</body>

The contents of the remote server-side Js.txt are:
var dylan= {name: "Dylan", email:dylan@163.com}
I feel this way more concise. Oh. Of course, the reader can choose the implementation way according to the actual situation.
How, in fact, it is very simple, I have seen many people do not want to face up to Ajax technology bottlenecks exist, in fact, Ajax should be Ajax rather than Ajax, highlighting the first A is to emphasize the fact that Ajax is a way to develop asynchronous transmission, rather than exactly what kind of technology used.
In fact, after the JSON data format, there is a more bull X "Jsonp", and can also achieve Ajax cross-domain communication. In fact, JSONP is not a data format, but the second way I introduced the improvement. Starting with the jQuery1.2 version, JQuery has local support for the JSONP callback.

Related Article

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.