我想出$ .ajax而不是getJSON來進行調試。因爲getJSON在IE(6,7或8)中沒有報告錯誤,所以我試圖弄清楚爲什麼jQuery插件沒有在IE中將返回的圖像繪製到屏幕上,而是在其他瀏覽器中。
所以我試了這個。有趣的是,它在IE中遇到錯誤事件,但不是Firefox,safari和其他的,我不知道爲什麼(這段代碼工作得很好,並且在FireFox和其他版本中呈現我的數據很好)。我知道我返回的JSON是有效的:
$.ajax({
type: "GET",
url: "http://localhost:59396/sss/sssHandler.ashx?action=getproducts&ids=" + ids,
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
alert(data.length);
carousel.size(allProductIDs.length);
if (numberOfImagesLeftToShow < numberOfImagesToDisplay) {
first += (numberOfImagesToDisplay - numberOfImagesLeftToShow);
}
var d = 0;
for (var i = first; i <= last; i++) {
if (d != undefined) {
// add data using index of the array returned by JSON (which starts at 0)
carousel.add(i, decode(data[d].ImageTag));
}
// set to last ProductID showing in Carousel
if (i == last) { lastProductID = parseFloat(data[d].ProductID); }
d++;
}
},
error: function() {
alert("An error has occurred. Please try again.");
}
});
我不知道自己還能做些什麼來解決IE的原因是有與返回的JSON這麼多的麻煩,或只是執行或者使用OR的getJSON此功能(數據) 。我在響應中也設置了標題而不是緩存。這不是問題。 問題出在什麼原因,IE拒絕在響應中輸入我的函數(數據)。
下面是返回的JSON這說明有效的(即使http://www.jsonlint.com/檢查的話):
[
{
"ImageTag": "\u003cdiv class=\"CarouselItem
\"\u003e<p><img src="http://www.xxx.com/image/
2568.jpg" alt=""></p>\u003cp\u003e\u003ca href=
\"Bear-10.prod\"\u003eTeddy Bear\u003c/a\u003e\u003c/p\u003e\u003cp
\u003e$20.95\u003c/p\u003e\u003cdiv\u003e",
"ProductID": "540"
},
{
"ImageTag": "\u003cdiv class=\"CarouselItem
\"\u003e<p><img src="http://www.xxx.com/image/
50.jpg" alt=""></p>\u003cp\u003e\u003ca href=
\"Pendant362.prod\"\u003eBirthday\u003csup\u003e©\u003c/sup
\u003Necklace\u003c/a\u003e\u003c/p\u003e\u003cp\u003e$36.95\u003c/p
\u003e\u003cdiv\u003e",
"ProductID": "83"
}
]
在我.ashx的
我只是有以下主要代碼產品的響應
context.Response.ContentType = "application/json";
context.Response.Charset = Encoding.UTF8.ToString();
context.Response.Cache.SetNoStore();
context.Response.Cache.SetExpires(DateTime.MinValue);
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.Cache.SetValidUntilExpires(false);
context.Response.Expires = -1;
context.Response.ExpiresAbsolute = DateTime.MinValue;
context.Response.AddHeader("Cache-Control", "no-cache");
context.Response.AddHeader("Cache-Control", "post-check=0, pre-check=0");
context.Response.AddHeader("Pragma", "no-cache");
...
string jsonString = imageList.ToJSON(); <-- uses the .NET 3.5 JavaScriptSerializer
context.Response.Write(jsonString);
也,如果您將response.ContentType指定爲「application/json」或「text/plain」,似乎並不重要,因爲至少我在FireFox中返回並分析了有效數據。
跑了它。相同的結果。在FireFox中運行良好,但不是IE8。它遇到錯誤:並且我得到Line:163 錯誤:系統錯誤:-1072896658。第163行是我的錯誤:function(){ alert(「發生錯誤,請重試。」); } – PositiveGuy 2009-07-06 18:57:37
實際上我確實想返回那裏的HTML,它只會是img標籤...沒什麼重要的。 – PositiveGuy 2009-07-06 19:06:24