C# JavaScript运行时错误:无效字符

C# JavaScript运行时错误:无效字符,c#,javascript,jquery,asp.net,ajax,C#,Javascript,Jquery,Asp.net,Ajax,我正在尝试从Ajax访问ASP.NET处理程序。我使用锚标记将数据传递给它,并返回一些测试字符串。代码如下: Ajax代码 function incvote(topicid) { $.ajax({ type: "POST", url: "WCAccess.ashx", data: JSON.stringify({ "desid": "topicid" }), contentType: "application/json; c

我正在尝试从Ajax访问ASP.NET处理程序。我使用锚标记将数据传递给它,并返回一些测试字符串。代码如下:

Ajax代码

function incvote(topicid) {
    $.ajax({
        type: "POST",
        url: "WCAccess.ashx",
        data: JSON.stringify({ "desid": "topicid" }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            return response.d;
        },
        error: function (xhr, ajaxOptions, thrownError) {

            alertify.error("Some error occured. Please try again.");
        },
        //async: true
    });
public class WCAccess : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
        string jsonString = string.Empty;
        List<ArgsVals> argsList = new List<ArgsVals>();
        context.Request.InputStream.Position = 0;
        using (var inputStream = new System.IO.StreamReader(context.Request.InputStream))
        {
            jsonString = inputStream.ReadToEnd();
        }

        argsList = jsonSerializer.Deserialize<List<ArgsVals>>(jsonString);
        string resp = "";
        foreach (ArgsVals arg in argsList)
        {
            resp += arg.desid;
        }
        context.Response.Write("<text>"+resp+"</text>");
    }

    public bool IsReusable {
        get {
            return false;
        }
    }
    public class ArgsVals
    {
        public string desid { get; set; }
        //public int vtype { get; set; }
    }
}
处理程序代码

function incvote(topicid) {
    $.ajax({
        type: "POST",
        url: "WCAccess.ashx",
        data: JSON.stringify({ "desid": "topicid" }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            return response.d;
        },
        error: function (xhr, ajaxOptions, thrownError) {

            alertify.error("Some error occured. Please try again.");
        },
        //async: true
    });
public class WCAccess : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
        string jsonString = string.Empty;
        List<ArgsVals> argsList = new List<ArgsVals>();
        context.Request.InputStream.Position = 0;
        using (var inputStream = new System.IO.StreamReader(context.Request.InputStream))
        {
            jsonString = inputStream.ReadToEnd();
        }

        argsList = jsonSerializer.Deserialize<List<ArgsVals>>(jsonString);
        string resp = "";
        foreach (ArgsVals arg in argsList)
        {
            resp += arg.desid;
        }
        context.Response.Write("<text>"+resp+"</text>");
    }

    public bool IsReusable {
        get {
            return false;
        }
    }
    public class ArgsVals
    {
        public string desid { get; set; }
        //public int vtype { get; set; }
    }
}
公共类WCAccess:IHttpHandler{
公共void ProcessRequest(HttpContext上下文){
JavaScriptSerializer jsonSerializer=新的JavaScriptSerializer();
string jsonString=string.Empty;
列表

但似乎什么都不起作用。同样的错误仍然存在。

看起来您试图在响应中返回HTML/XML而不是JSON

context.Response.Write("<text>"+resp+"</text>");
context.Response.Write(“+resp+”);

阅读要发送回的消息的结构。

data:JSON.stringify({desid:topicid}),已经尝试了所有引用和非引用的变体。没有任何效果。alertify.error()这是做什么的?尝试在context.Response.Write(“+resp+”)之后添加到处理程序代码Response.End()。谢谢,这就是实际发生的情况。我将数据类型更改为
html
,它成功了。我想问的另一件事是,如何使用处理程序的此响应更改锚定标记的文本?我是这样做的:`document.getElementById(“suplink”).innerHTML=response.d;`但是它将文本更改为
undefined
,而不是处理程序传递的
test
。请帮助。您已经在使用jQuery,因此请查看addClass和removeClass函数,并添加一些CSS作为颜色。此外,您还应该使用类似$('#suplink').html(response)的东西;好的,现在我明白了。将数据类型转换为html后的一个问题是,我无法在服务器端的
argsList
中反序列化和存储数据。它显示0个元素。但是获取数据时使用json字符串。