jQuery JSON 文件扩展名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6699091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:14:59  来源:igfitidea点击:

JSON File Extension

jsonjqueryfile-extension

提问by techlead

I've been saving all my json files with .txt extension and they worked with jquery ajax calls.

我一直在用 .txt 扩展名保存我的所有 json 文件,并且它们使用 jquery ajax 调用。

When I change the extension to .json and in my jquery ajax call -- jQuery.ajax()-- I specify

当我将扩展名更改为 .json 并在我的 jquery ajax 调用中 -- jQuery.ajax()-- 我指定

  • dataType: "json",
  • contentType: "application/json; charset=utf-8",
  • 数据类型:“json”,
  • contentType: "application/json; charset=utf-8",

the files no longer work. Why so?

这些文件不再起作用。为什么这样?

Shouldn't all json files have an extension .json? I'm using IIS server.

不是所有的 json 文件都应该有一个扩展名 .json 吗?我正在使用 IIS 服务器。

JSON

JSON

{ "rows": [ 
  {"row":[ 
    {"cells": [ 
      {"data": "Edit"}, 
      {"data": "030194"} 
    ]} 
  ]}, 
  {"row":[ 
    {"cells": [ 
      {"data": "Add"}, 
      {"data": "030194"} 
    ]} 
  ]}  
]}

jQuery

jQuery

jQuery.ajax ({ 
  type: "GET", 
  url: "localhost/ABC.json", 
  dataType: "json", 
  contentType: "application/json; 
  charset=utf-8", 
  cache: "false", 
  success: function(response){}  
});

Can someone please tell me why extension .json is not working? It works if I change it to .txt

有人可以告诉我为什么扩展 .json 不起作用吗?如果我将其更改为 .txt,它会起作用

回答by aggregate1166877

The correct extension is .json, and the mime type is application/json(reference: this Wikipedia page). Generally speaking, however, it should work with any extension so long as your data structure is valid and your web server is doing what it's supposed to.

正确的扩展名是.json,并且 mime 类型是application/json(参考:这个维基百科页面)。然而,一般来说,只要您的数据结构有效并且您的 Web 服务器正在执行它应该做的事情,它就应该适用于任何扩展。

回答by ashes999

IIS comes bundled with a bunch of MIME type handlers. This means when you enter a URL that ends with, for example, .png, IIS knows that this is an image, and dispatches the appropriate response to tell the client (browser) that it's an image (so it can be rendered as such).

IIS 捆绑了一堆 MIME 类型的处理程序。这意味着当您输入以 结尾的 URL 时,例如,.pngIIS 知道这是一个图像,并分派适当的响应来告诉客户端(浏览器)它是一个图像(因此它可以这样呈现)。

.jsondoesn't have a MIME type handler by default. You need to set one up:

.json默认情况下没有 MIME 类型处理程序。你需要设置一个:

  • Load IIS Manager
  • Browse the tree nodes up to your web app or website
  • Double click on the MIME Types feature (lower pane)
  • Click Add (RHS under "Add", or right-click and pick Add)
  • Put .txtunder the extension field and application/jsonas the MIME type
  • 加载 IIS 管理器
  • 浏览直到您的 Web 应用程序或网站的树节点
  • 双击 MIME 类型功能(下窗格)
  • 单击“添加”(“添加”下的 RHS,或右键单击并选择“添加”)
  • 放在.txt扩展字段下并application/json作为 MIME 类型

You're done! Try requesting the JSON file in the browser; if it renders correctly (as text), awesome. If not, you may have to tell IIS to render it as text/plaininstead.

你完成了!尝试在浏览器中请求 JSON 文件;如果它呈现正确(作为文本),真棒。如果没有,您可能必须告诉 IIS 将其呈现为text/plain

回答by linuxtotal

dataType: "json" is not to specify the type of a file is to specify the kind of data that the server will return. From the server side, for example if you are using php, you have to return the json string using the function "json_encode" so it can be accepted by dataType: "json".

dataType: "json" 不是指定文件的类型是指定服务器将返回的数据类型。从服务器端,例如,如果您使用的是 php,则必须使用函数“json_encode”返回 json 字符串,以便 dataType:“json” 接受它。