Ajax 跨域操作
浏览器出于安全考虑默认禁用xmlhttp的跨域,但一般不同业务接口会放在不同域名下,支持跨域调用也是很有必要的。
其中一种方案是用下载javascript来实现,不符合HTTP 规范做法,仅供参考慎用。 下面的代码是一个消息通知,就是在以下多个站点下发布同一个信息,后面还有个“我知道了”点击消失,而且第二次不能显示出来了。豆瓣上有类似功能,汪汪同学就也要一个。下面的ashx文件输出js代码。js生成html及js跨域操作。
<
%@ WebHandler Language="C#" Class="ClubNoticeHandler" %>
using System;
using System.Web;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
public class ClubNoticeHandler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
string TxzId = context.Request.QueryString["TxzId"];
string NoticeId = context.Request.QueryString["NoticeId"];
if (!string.IsNullOrEmpty(TxzId))
{
if (!string.IsNullOrEmpty(NoticeId)) //处理一下啊
{
HaveSeeNotice(Convert.ToInt32(TxzId), Convert.ToInt32(NoticeId));
}
else //输出脚本
{
context.Response.Write( GetScript());
}
}
}
public bool IsReusable {
get {
return false;
}
}
private String GetScript()
{
//实现跨域提交,不用jquery
string script = " function IhavSeeThis(TxzId, NoticeId) {" +
" document.getElementById('ClubNotice_' + NoticeId).style.display = \"none\";" +
" var sc= document.createElement(\"script\");"+
" sc.type=\"text/javascript\";" +
" sc.src=\"ClubNoticeHandler.ashx?TxzId=\" + TxzId + \"&NoticeId=\" + NoticeId;" +
" document.body.appendChild(sc);" +
" }";
//" $.get(" +
// " \"ClubNoticeHandler.ashx?TxzId=\" + TxzId + \"&NoticeId=\" + NoticeId, function() { alert(1) }" +
// " )" +
string Html="";
int TxzId = 3866;
List<ClubNotice> club = GetNoSeeNotice(TxzId);
foreach (ClubNotice cl in club)
{
Html+= "<div id=\"ClubNotice_" + cl.Notice_Id + "\">内容:" + cl.Notice_Content + "<a href='javascript:void(0)' onclick=\"IhavSeeThis(" + TxzId.ToString() + "," + cl.Notice_Id + ")\"'>我看过了<a></div><br/>";
}
string HtmlScript = " document.write(\""+Html.Replace("\"","\\\"")+"\");";
return script + HtmlScript;
}
public void HaveSeeNotice(int TxzId, int NoticeId)
{
string SqlText = "insert into [UserNotice] ([UserNot_TxzId] ,[UserNot_NotId]) values (@UserNot_TxzId,@UserNot_NotId)";
SqlParameter[] param = new SqlParameter[2];
param[0] = new SqlParameter("@UserNot_TxzId", TxzId);
param[1] = new SqlParameter("@UserNot_NotId", NoticeId);
DBHelper.DBHelper.ExecuteNonQuery(DBHelper.DBHelper.Ncuhome2006Conn, CommandType.Text, SqlText, param);
}
public List<ClubNotice> GetNoSeeNotice(int TxzId)
{
List<ClubNotice> club = new List<ClubNotice>();
string SqlText = "select * from [Notice] where Notice_DurToTime >'" + DateTime.Now.ToString() + "' and Notice_Visible=1 and " +
" Notice_Id not in (select UserNot_NotId from UserNotice where UserNot_TxzId=" + TxzId.ToString() + ")";
SqlDataReader reader = DBHelper.DBHelper.ExecuteReader(SqlText, DBHelper.DBHelper.Ncuhome2006ConnectionString);
while (reader.Read())
{
ClubNotice c = new ClubNotice();
c.Notice_Content = reader["Notice_Content"].ToString();
c.Notice_CreateTime = Convert.ToDateTime(reader["Notice_CreateTime"]);
c.Notice_Title = reader["Notice_Title"].ToString();
c.Notice_Id = Convert.ToInt32(reader["Notice_Id"]);
club.Add(c);
}
reader.Close();
return club;
}
public class ClubNotice
{
public int Notice_Id { get; set; }
public string Notice_Title { get; set; }
public string Notice_Content { get; set; }
public DateTime Notice_CreateTime { get; set; }
}
}
前端页面引用只是需要添加 <script src=' http://club.ncuhome.cn/ClubNoticeHandler.ashx?txzid=222'/> 。