• <small id='Klgpy'></small><noframes id='Klgpy'>

      <bdo id='Klgpy'></bdo><ul id='Klgpy'></ul>

      1. <i id='Klgpy'><tr id='Klgpy'><dt id='Klgpy'><q id='Klgpy'><span id='Klgpy'><b id='Klgpy'><form id='Klgpy'><ins id='Klgpy'></ins><ul id='Klgpy'></ul><sub id='Klgpy'></sub></form><legend id='Klgpy'></legend><bdo id='Klgpy'><pre id='Klgpy'><center id='Klgpy'></center></pre></bdo></b><th id='Klgpy'></th></span></q></dt></tr></i><div id='Klgpy'><tfoot id='Klgpy'></tfoot><dl id='Klgpy'><fieldset id='Klgpy'></fieldset></dl></div>
        <tfoot id='Klgpy'></tfoot>
        <legend id='Klgpy'><style id='Klgpy'><dir id='Klgpy'><q id='Klgpy'></q></dir></style></legend>

        浏览器对 json ajax 响应的 Content-Type 标头有什么要

        时间:2023-10-14

        <small id='As2uE'></small><noframes id='As2uE'>

      2. <legend id='As2uE'><style id='As2uE'><dir id='As2uE'><q id='As2uE'></q></dir></style></legend>
        • <bdo id='As2uE'></bdo><ul id='As2uE'></ul>

            • <i id='As2uE'><tr id='As2uE'><dt id='As2uE'><q id='As2uE'><span id='As2uE'><b id='As2uE'><form id='As2uE'><ins id='As2uE'></ins><ul id='As2uE'></ul><sub id='As2uE'></sub></form><legend id='As2uE'></legend><bdo id='As2uE'><pre id='As2uE'><center id='As2uE'></center></pre></bdo></b><th id='As2uE'></th></span></q></dt></tr></i><div id='As2uE'><tfoot id='As2uE'></tfoot><dl id='As2uE'><fieldset id='As2uE'></fieldset></dl></div>

                  <tbody id='As2uE'></tbody>
                <tfoot id='As2uE'></tfoot>

                  本文介绍了浏览器对 json ajax 响应的 Content-Type 标头有什么要求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在返回一些需要由 javascript 处理的 json 作为对 XMLHTTPRequest 的响应.

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

                  在将其传递给我的 javascript 之前.

                  如果我将响应的内容类型设置为正确"应用程序/json",所有浏览器,但 Firefox 将接受它并将其传递给我的 JS,没有问题.但是,Firefox 会要求将响应保存或打开为文件.

                  什么是正确的跨浏览器内容类型?

                  解决方案

                  您可以通过使用jQuery funcion parseJSON - http://api.jquery.com/jQuery.parseJSON/

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

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

                  在 FF 和 IE8 中针对以下简单 JSON 结果进行了测试(除了 Chrome 解决了哪个问题),对于其他浏览器和更复杂的响应,无法保证...

                  <小时>

                  注意:我认为这种情况下的内容类型是 text/plain 或 text/html - 我使用了以下 ASP.Net MVC 函数来返回结果

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

                  我返回 JSON 对象的位置,如

                  System.Web.Script.Serialization.JavaScriptSerializer jsonSerializer= 新 System.Web.Script.Serialization.JavaScriptSerializer();var jsonResponse = jsonSerializer.Serialize(新 { IArticleMediaId = 0, ImageUrl = Url.Content(fullImgPath)});返回内容(jsonResponse);

                  I am returning some json which needs to be handled by javascript as the response to an XMLHTTPRequest.

                  If I set the response's content type to "text/plain", all browsers but Chrome will accept it and pass it to my JS with no problem. However, Chrome will wrap the response in

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

                  before passing it to my javascript.

                  If I set the response's content type to the "proper" "application/json" all browsers but Firefox will accept it and pass it to my JS with no problem. Firefox, however will ask to save or open the response as a file.

                  What's the correct, cross-browser Content-Type?

                  解决方案

                  You may solve the issue by parsing the response into the JSON object by using jQuery funcion parseJSON - http://api.jquery.com/jQuery.parseJSON/

                  The parameter you pass into the function is the JSON object string, which you extract from the response data:

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

                  Tested (besides Chrome which problem this solves) in FF and IE8 for the following simple JSON result, for other browsers and more complex responses no guarantees...


                  NOTE: the content type in this case is text/plain or text/html I think - I've used the following ASP.Net MVC function to return the result

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

                  Where I returned the JSON object like

                  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);
                  

                  这篇关于浏览器对 json ajax 响应的 Content-Type 标头有什么要求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                • <tfoot id='urPve'></tfoot>

                      <legend id='urPve'><style id='urPve'><dir id='urPve'><q id='urPve'></q></dir></style></legend>
                        <tbody id='urPve'></tbody>
                          <bdo id='urPve'></bdo><ul id='urPve'></ul>

                          <small id='urPve'></small><noframes id='urPve'>

                        • <i id='urPve'><tr id='urPve'><dt id='urPve'><q id='urPve'><span id='urPve'><b id='urPve'><form id='urPve'><ins id='urPve'></ins><ul id='urPve'></ul><sub id='urPve'></sub></form><legend id='urPve'></legend><bdo id='urPve'><pre id='urPve'><center id='urPve'></center></pre></bdo></b><th id='urPve'></th></span></q></dt></tr></i><div id='urPve'><tfoot id='urPve'></tfoot><dl id='urPve'><fieldset id='urPve'></fieldset></dl></div>