我已经看到很多其他人在SO和其他地方都遇到过这个问题,但是没有一个给定的解决方案似乎适合我。
我对ASHX处理程序进行了jQuery AJAX调用。该代码适用于Firefox和Chrome,但不适用于IE(版本8,9或10)。
看起来像这样(简化):
var val = $(this).val();
$.ajax({
url: '/Source/Handlers/Search.ashx?q=' + val,
type: "GET",
cache: false,
dataType: 'json',
error: function(w,t,f) {
alert(w + "\n" + t + "\n" + f);
},
success: function (data) {
...
}
});
当我在IE中运行代码时,错误处理程序启动并说
[object Object]
error
error
有用吗?
ASHX处理程序是这样的(再次简化):
public class Search : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.ContentType = "application/json";
context.Response.Buffer = false;
JavaScriptSerializer serializer = new JavaScriptSerializer();
List<Suggestions> jsonResponse = new List<Suggestions>();
// do some stuff
context.Response.Write(serializer.Serialize(jsonResponse));
}
}
我尝试将“GET”交换为“POST”,我尝试启用并禁用“缓存”属性,我尝试了不同的dataType设置,例如“json”,“xml”和“text”。
使用HTTP Fiddler我可以看到json响应确实回来了,但IE似乎无法处理它。
有什么想法吗?
修改
以下是来自HTTP Fiddler的TextView中的JSON响应:
543
[{"title":"GAMES","results":[{"title":"abcdefg™","link":"http://www.abcdefg.com/en_gb/abcdefg/","image":"/uploadedImages/abcdefg/games/1-Game_Folder_Master(1)/Config/abcdefg(1).jpg","total":""},{"title":"abcdefg","link":"http://www.abcdefg.com/en_gb/abcdefg","image":"/uploadedImages/abcdefg/games/1-Game_Folder_Master(1)/Config/abcdefg.jpg","total":""},{"title":"abcdefgg","link":"/Retailers/?game=1432&platform=0","image":"/uploadedImages/abcdefg/games/1-Game_Folder_Master(1)/Config/abcdefg.jpg?n=9736","total":""},{"title":"abcdefg","link":"http://www.abcdefg.com","image":"/uploadedImages/abcdefg/games/1-Game_Folder_Master(1)/Config/abcdefg.jpg","total":""},{"title":"abcdefg","link":"/Retailers/?game=1763&platform=0","image":"/uploadedImages/abcdefg/games/1-Game_Folder_Master(1)/Config/abcdefg_promo.jpg","total":""}],"total":"24","link":"/Search/?q=total"},{"title":"MEDIA","results":[{"title":"Videos","link":"/Search/?q=total","image":"","total":"1"},{"title":"Screenshots","link":"/Search/?q=total","image":"","total":"35"}],"total":null,"link":null}]
0
编辑2
嗯,我更新了$ .ajax调用以警告w.status并返回“404”。困惑 - 当Fiddler将响应显示为200时,如何在状态中返回404?
答案 0 :(得分:1)
我依旧记得这样的问题。 你能尝试添加
吗?contentType: "application/json; charset=utf-8"
到ajax请求属性。我认为这解决了当时的问题。
答案 1 :(得分:0)
尝试添加data : {q : val}
,看看是否能解决问题:
var val = $(this).val();
$.ajax({
url: '/Source/Handlers/Search.ashx',
type: "GET",
data : {q : val}, //<--------send it this way
cache: false,
dataType: 'json',
error: function(w,t,f) {
alert(w + "\n" + t + "\n" + f);
},
success: function (data) {
...
}
});
答案 2 :(得分:0)
经过大量的反复试验后,我发现了问题。使用IE时,我需要在从ASHX处理程序返回的数据对象上调用JSON.parse(data)。为此,请使用非jQuery方法调用Ajax。这意味着我有两个不同的Javascript块 - 一个用于IE,另一个用于其他浏览器。
if ($.browser.msie) {
var xhReq = new XMLHttpRequest();
xhReq.open("GET", "/Source/Handlers/Search.ashx?q=" + val + "&d=" + dateObj.getTime(), false);
xhReq.send(null);
AjaxSuccess(xhReq.responseText);
} else {
$.ajax({
url: '/Source/Handlers/Search.ashx',
type: "GET",
cache: false,
dataType: 'json',
data: { q: val },
contentType: "application/json;",
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.responseText);
},
success: function (data) {
...
}
});
}
function AjaxSuccess(data) {
data = JSON.parse(data);
...
}
奇怪的是,JSON.parse()方法在Firebug中引发了“意外字符”错误。
答案 3 :(得分:0)
也许编码有问题。试试这个
$.ajax({
type: 'GET',
url: encodeURI(url ),
dataType: 'text',
success: function (data) {
result = JSON.parse(JSON.parse(data))
}
});