2010-06-08 23 views
8

我正在返回一些需要由javascript处理的json作为对XMLHTTPRequest的响应。浏览器对json ajax响应上的Content-Type标头需要什么?

如果我将响应的内容类型设置为“text/plain”,除Chrome之外的所有浏览器都会接受它,并将它传递给我的JS,没有任何问题。但是,Chrome会将其响应包含在

<pre style="word-wrap: break-word; white-space: pre-wrap;"> 

之前将它传递给我的JavaScript。

如果我将响应的内容类型设置为“正确的”“application/json”所有浏览器,但Firefox将接受它并将它传递给我的JS,没有任何问题。不过,Firefox会要求将文件保存或打开。

什么是正确的,跨浏览器的Content-Type?

+0

可能重复的[*正确* JSON内容类型?](http://stackoverflow.com/questions/477816/the-right-json-content-type) – 2010-06-08 23:51:58

+1

也许。除了那个线程有*错误*的答案(如果你在那里使用答案,firefox的行为就像一个娇生惯养的小孩) – 2010-06-09 14:59:40

+0

Firefox如何行事不端?如果您指的是保存和下载,请尝试在此处提及的“在浏览器中打开”建议:http://stackoverflow.com/questions/94767 – 2010-06-10 14:23:49

回答

7

您可以通过解析响应为JSON对象通过使用jQuery funcion parseJSON解决问题 - http://api.jquery.com/jQuery.parseJSON/

传递到函数的参数是JSON对象的字符串,你从响应数据提取:

function AjaxResponse (data) { // AJAX post callback 
    var jsonResult = $.parseJSON(data.substring(data.indexOf("{"), data.lastIndexOf("}") + 1)); 
} 

测试(除了铬哪个问题都不能解决)的FF和IE8以下简单的JSON结果,对其他浏览器和更复杂的反应没有保证......


注:在这种情况下,内容类型为text/plain text/html或我想 - 我用下面的ASP.Net MVC函数返回的结果

ContentResult System.Web.Mvc.Controller.Content(string content); 

当我回来JSON对象像

System.Web.Script.Serialization.JavaScriptSerializer jsonSerializer 
    = new System.Web.Script.Serialization.JavaScriptSerializer(); 
var jsonResponse = jsonSerializer.Serialize(
    new { IArticleMediaId = 0 
     , ImageUrl = Url.Content(fullImgPath) 
     }); 
return Content(jsonResponse); 
0

我使用置换结果象下面这样:

this.jjAjaxFileUpload = function (inputFileId,inputTextSelector,viewImgSelector){ 
    $(this.selector).click(function(){ 
     if($("#"+inputFileId.replace("#", "")).val()==""){ 
      new jj("Chose a file at first").jjDialog(); 
      return; 
     } 
     $.ajaxFileUpload({ 
      url :'UploadServlet', 
      secureuri:false, 
      fileElementId:inputFileId.replace("#", ""), 
      dataType: 'JSON', 
      cache: false , 
      success: function(data){ 
       if(data!=null){ 
        data = data.replace('<pre style="word-wrap: break-word; white-space: pre-wrap;">',''); 
        data = data.replace('<PRE style="word-wrap: break-word; white-space: pre-wrap;">',''); 
        data = data.replace("<PRE>", '').replace("</PRE>", '').replace("<pre>", '').replace("</pre>", '').replace("upload/", '').replace("Upload/", ''); 
        data = data.replace("/", '').replace("/", '').replace("\\", ''); 
       }else{ 
        new jj('error.').jjDialog(); 
       } 
       $("#"+inputFileId.replace("#", "")).val('');      
       if(data!=""){ 
        if(data!="big"){ 
         $(inputTextSelector).val(data); 
         if(viewImgSelector!=null){ 
          $(viewImgSelector).attr('src','upload/'+data); 
         } 
        }else{ 
         new jj('Error : file is too big!!!').jjDialog(); 
        } 
       }else{ 
        new jj('error').jjDialog(); 
       } 
      } 
     }); 
    }); 
}; 

需要注意的是:

inputTextSelector是一个文本字段,以存储上传的文件名

viewImgSelector是img标签预览上传图片的ID。

相关问题