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

2023-05-16前端开发问题
2

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

问题描述

限时送ChatGPT账号..

我正在返回一些需要由 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 标头有什么要求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

ajax请求获取json数据并处理的实例代码
ajax请求获取json数据并处理的实例代码 $.ajax({ type: 'GET', url: 'https://localhost:44369/UserInfo/EditUserJson',//请求数据 data: json,//传递数据 //dataType:'json/text',//预计服务器返回的类型 timeout: 3000,//请求超时的时间 //回调函数传参 suc...
2024-11-22 前端开发问题
215

layui 单选框、复选框、下拉菜单不显示问题如何解决?
1. 如果是ajax嵌套了 页面, 请确保 只有最外层的页面引入了layui.css 和 layui.js ,内层页面 切记不要再次引入 2. 具体代码如下 layui.use(['form', 'upload'], function(){ var form = layui.form; form.render(); // 加入这一句});...
2024-11-09 前端开发问题
313

Rails 3.1 ajax:成功处理
Rails 3.1 ajax:success handling(Rails 3.1 ajax:成功处理)...
2024-04-20 前端开发问题
11

使用 will_paginate gem 在 Rails 3.2.3 中调用 Ajax
Ajax call in rails 3.2.3 with will_paginate gem(使用 will_paginate gem 在 Rails 3.2.3 中调用 Ajax)...
2024-04-20 前端开发问题
6

在某些(不是全部)浏览器中读取 D3 javascript 中的 JSON 文件时出现问题
Problems reading JSON file in D3 javascript in some (not all) browsers(在某些(不是全部)浏览器中读取 D3 javascript 中的 JSON 文件时出现问题)...
2024-04-19 前端开发问题
8

使用视图附加或嵌入节点添加表单
Attach or embed node add form with a view(使用视图附加或嵌入节点添加表单)...
2024-04-19 前端开发问题
9