ashx如何执行JavaScript代码
如下面的代码, 当执行完成后,在面会出现"<script>alert('删除成功!'); window.history.go(-1);</script>"字样, 而不是执行该段JavaScript代码..
要怎么做才能让它执行?
<%@ WebHandler Language="C#" Class="Style_Sheet_Attachment_Del" %>using System;using System.Web;using System.Data;using System.Data.SqlClient;public class Style_Sheet_Attachment_Del : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; try { if (context.Request.QueryString["Style_Sheet_Attachment_Id"] == null) { context.Response.Write("<script>alert('请以 QuantityString 方式传入 Style_Sheet_Attachment_Id'); window.history.go(-1);</script>"); } else { string cmdText = "DELETE FROM Style_Sheet_Attachment WHERE Id = @Id"; DataHelper.SqlHelper.ExecuteNonQuery(CommonFun.ConnectionString, CommandType.Text, cmdText, new SqlParameter("@Id", context.Request.QueryString["Style_Sheet_Attachment_Id"])); context.Response.Write("<script>alert('删除成功!'); window.history.go(-1);</script>"); } } catch (Exception ex) { context.Response.Write(ex.Message); } } public bool IsReusable { get { return false; } }}
<%@ WebHandler Language="C#" Class="Style_Sheet_Attachment_Del" %>using System;using System.Web;using System.Data;using System.Data.SqlClient;public class Style_Sheet_Attachment_Del : IHttpHandler { public void ProcessRequest(HttpContext context) { try { if (context.Request.QueryString["Style_Sheet_Attachment_Id"] == null) { context.Response.Write("<script>alert('请以 QuantityString 方式传入 Style_Sheet_Attachment_Id'); window.history.go(-1);</script>"); } else { string cmdText = "DELETE FROM Style_Sheet_Attachment WHERE Id = @Id"; DataHelper.SqlHelper.ExecuteNonQuery(CommonFun.ConnectionString, CommandType.Text, cmdText, new SqlParameter("@Id", context.Request.QueryString["Style_Sheet_Attachment_Id"])); context.Response.Write("<script>alert('删除成功!'); window.history.go(-1);</script>"); } } catch (Exception ex) { context.Response.Write(ex.Message); } } public bool IsReusable { get { return false; } }}
[解决办法]
直接把context.Response.ContentType = "text/plain";去掉就可以
恩 对的
不然直接输出文本