jquery+ajax实现跨域请求的方法
人气:0本文实例讲述了jquery+ajax实现跨域请求的方法。分享给大家供大家参考。具体实现方法如下:
说明:这里的dataType 为 "jsonp" ;type 只能为 GET
前台请求代码如下:
复制代码 代码如下:
$.ajax({
type: "GET",
url: "http://www.xxx.com/Rest/ValidAccountsExists.aspx?accounts=admin",
dataType: "jsonp",
jsonp: "jsoncallback",
success: function (result) {
alert(result.Success);
alert(result.Content);
},
type: "GET",
url: "http://www.xxx.com/Rest/ValidAccountsExists.aspx?accounts=admin",
dataType: "jsonp",
jsonp: "jsoncallback",
success: function (result) {
alert(result.Success);
alert(result.Content);
},
error: function (result, status) {
//处理错误
}
});
后台处理代码 ValidAccountsExists.aspx如下:
复制代码 代码如下:
string accounts = GameRequest.GetQueryString("accounts");
string jsoncallback = GameRequest.GetQueryString("jsoncallback");
Response.ContentEncoding =System.Text.Encoding.UTF8;
Response.ContentType = "application/json";
Response.Write(jsoncallback + "({\"Success\":\"True\",\"Content\":\"" + accounts + "\"})");
Response.End();
string jsoncallback = GameRequest.GetQueryString("jsoncallback");
Response.ContentEncoding =System.Text.Encoding.UTF8;
Response.ContentType = "application/json";
Response.Write(jsoncallback + "({\"Success\":\"True\",\"Content\":\"" + accounts + "\"})");
Response.End();
希望本文所述对大家的jQuery程序设计有所帮助。
加载全部内容