2009-07-06 21 views
1

我想出$ .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&lt;p&gt;&lt;img src=&quot;http://www.xxx.com/image/ 
2568.jpg&quot; alt=&quot;&quot;&gt;&lt;/p&gt;\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&lt;p&gt;&lt;img src=&quot;http://www.xxx.com/image/ 
50.jpg&quot; alt=&quot;&quot;&gt;&lt;/p&gt;\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中返回并分析了有效数据。

回答

1

我怀疑它是json的数据,先让我们试试看关于这个问题。如果您简单地返回,请求是否有效?

[ 
    { 
     "ImageTag": "imagePath", 
     "ProductID": "540" 
    }, 
    { 
     "ImageTag": "imagePath", 
     "ProductID": "83" 
    } 
] 

如果它确实存在,那么即对该编码中的一些有困难。

N.B我也尝试了你在jsonlint上粘贴的json,除非我在第二个图像中删除了imageTag的最后一行,否则无法验证它。

你真的需要在json中返回html吗?如果是这样,为什么不只是做一个HTML的GET作为你没有真正从一个轻量级的JSON结构

+0

跑了它。相同的结果。在FireFox中运行良好,但不是IE8。它遇到错误:并且我得到Line:163 错误:系统错误:-1072896658。第163行是我的错误:function(){ alert(“发生错误,请重试。”); } – PositiveGuy 2009-07-06 18:57:37

+0

实际上我确实想返回那里的HTML,它只会是img标签...没什么重要的。 – PositiveGuy 2009-07-06 19:06:24

1

不知道受益,如果这是关系到这个问题,但的contentType参数指定的MIME类型的数据是发送的到服务器,不被接收。

由于您没有发送任何东西(只是一个GET,查询字符串已经在URL中指定),您可能不需要参数contentTypedata

0

我认为jQuery使用eval(“(”+ data +“)”),然后将结果传递给您的成功函数,因为您已指定您期待JSON数据。可能有必要将此内容类型更改为“文本”,然后在成功回调中使用JSON.parse进行解析。只是一个想法!

0

固定它,

移除:

context.Response.Charset = Encoding.UTF8。的ToString();

已更改: context.Response.ContentType =“application/json; charset = utf-8”;

相关问题