JSON File Extension

蓝咒 提交于 2020-01-01 04:52:09

问题


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

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

  • dataType: "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

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

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


回答1:


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.




回答2:


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

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

  • 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 .txt under the extension field and application/json as the MIME type

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/plain instead.




回答3:


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".



来源:https://stackoverflow.com/questions/6699091/json-file-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!