jQuery $.getJSON 跨域
代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(function(){
$.getJSON("http://localhost:60628/website/Default.aspx?jsoncallback=?",
function(json){
alert(json.n7);
});
});
</script>
</head>
<body>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(function(){
$.getJSON("http://localhost:60628/website/Default.aspx?jsoncallback=?",
function(json){
alert(json.n7);
});
});
</script>
</head>
<body>
</body>
</html>
主要注意一下这个参数:
jsoncallback=?
参数名可以任意,服务端要接收"jsoncallback"这个参数,jquery 会动态给这个参数不同的值。
Default.aspx
代码
using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string jsoncallback =Request.QueryString["jsoncallback"];
Response.Write(jsoncallback+"({n7:\"9\"})");
}
}
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string jsoncallback =Request.QueryString["jsoncallback"];
Response.Write(jsoncallback+"({n7:\"9\"})");
}
}