Home >Web Front-end >JS Tutorial >jquery ajax cross-domain solution (json method)

jquery ajax cross-domain solution (json method)

高洛峰
高洛峰Original
2017-01-12 10:01:471311browse

Recently, many 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

must request the user’s json information and then process the data. At first, my colleagues and I tried many methods, using $.ajax() whether The get or post methods 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. The example is 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 processing in the ASPX.cs file is
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 codes and js errors will occur.

For more jquery ajax cross-domain solutions (json method) related articles, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn