Home >Web Front-end >JS Tutorial >jquery ajax cross-domain solution (json method)_jquery
Many places in recent projects developed by the company require cross-domain ajax requests, such as several sub-domain names
http://a.****.com/index123.aspx,
http://b .****.com/index2.aspx
You have to request the user's json information and then process the data. At first, my colleagues and I tried many methods. Using $.ajax(), whether it is the get or post method, will cause uri deny errors. After some GG, I found the solution and understood the reason.
Starting from jquery 1.2, .getJSON supports cross-domain operations. Cross-domain problems can be solved using the jquery.getJSON() method. Examples are as follows
Front Desk
JS code in HTML
function gettst2() {
$.getJSON("http://ucenter.xxxx.com.cn/ajax/test .aspx?callback=?", { id: "123456", site: "01" },
function(data) {
alert(data.htmls);
document.getElementById("shows" ).innerHTML = data.htmls;
});
}
gettst2();
The ASPX.cs file is processed as
string jsoncall = Request.QueryString("callback");
Response.Write(jsoncall "({htmls:test001})") ;
If you add html code, be sure not to add the /n symbol, otherwise garbled characters and js errors will occur.