我想從Ajax訪問ASP.NET處理程序。我使用錨標記將數據傳遞給它,並返回一些測試字符串。以下是代碼:JavaScript運行時錯誤:無效字符
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; }
}
}
一切工作順利,直到控制離開context.Response.Write()
聲明。數據很好地獲取數據resp
。但問題是,JavaScript顯示此錯誤:JavaScript runtime error: Invalid character
。
我搜索了SO和其他網站。我也試過這個解決方案:Ajax request has invalid characters 但似乎沒有任何工作。同樣的錯誤依然存在。
data:JSON.stringify({desid:topicid}), – Triode
已經嘗試過所有引用和未引用的變體。沒有工作。 –
alertify.error()這是幹什麼用的? – Triode