浏览器对 json ajax 响应的 Content-Type 标头有什么要求? [英] What do browsers want for the Content-Type header on json ajax responses?

查看:28
本文介绍了浏览器对 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 标头有什么要求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆