日期:2014-05-17  浏览次数:20449 次

ashx如何执行JavaScript代码
如下面的代码, 当执行完成后,在面会出现"<script>alert('删除成功!'); window.history.go(-1);</script>"字样, 而不是执行该段JavaScript代码..
要怎么做才能让它执行?
C# code
<%@ 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;
        }
    }

}


------解决方案--------------------
直接把context.Response.ContentType = "text/plain";去掉就可以
C# code

<%@ 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";去掉就可以
恩 对的
不然直接输出文本