javascript 数据类型“应用程序/json”与“json”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13735869/
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-10-26 19:39:19  来源:igfitidea点击:

dataType 'application/json' vs. 'json'

javascriptjqueryjson

提问by Isaac

Possible Duplicate:
$.ajax - dataType

可能重复:
$.ajax - dataType

I am using jQuery 1.8.2, and for some reason 'application/json'does not work, but 'json'works as dataTypeto a standard jqueryajaxcall. Is this a glitch? A version related difference? or is there an established difference between the two?

我使用的是 jQuery 1.8.2,由于某种原因'application/json'不起作用,但'json'可以作为dataType标准jqueryajax调用使用。这是故障吗?与版本相关的差异?或者两者之间是否存在既定差异?

$(document).ready(function() {
    $.ajax({
        type : "POST",
        url : '<c:url value="/url.htm" >',
        //dataType : "application/json", <-- does not work
        dataType: 'json' // <-- works
        success : function(data) {
            // do something          
        },
        error : function(data) {
            // do something else
        }
    });
});

回答by Musa

dataTypetakes json, it means the request expects a jsonresponse.

dataType接受 json,这意味着请求需要json响应。

contentTypetakes application/json, it means the request is sending jsondata

contentTypetake application/json,表示请求正在发送json数据

You can send as well as expect json in a request e.g.

您可以在请求中发送以及期望 json,例如

$.ajax({
    type : "POST",
    url : url,
    contentType : "application/json", 
    dataType: 'json',
    data: JSON.stringify({some: 'data'}),
    success : function(data) {
        // do something          
    },
    error : function(data) {
        // do something else
    }
});

here you're sending json and expecting xml

在这里你发送 json 并期待 xml

$.ajax({
    type : "POST",
    url : url,
    contentType : "application/json", 
    dataType: 'xml',
    data: JSON.stringify({xmlfile: 'file.xml'}),
    success : function(data) {
        // do something          
    },
    error : function(data) {
        // do something else
    }
});

and here you're sending x-www-form-urlencoded(jQuery automatically sets this for you), and expect json back

在这里你发送x-www-form-urlencoded(jQuery 自动为你设置),并期待 json 回来

$.ajax({
    type : "POST",
    url : url,
    dataType: 'json',
    data: {id: '1'},
    success : function(data) {
        // do something          
    },
    error : function(data) {
        // do something else
    }
});

contentType sets the ContentTypeHTTP request header, telling the server that the body of this request is of the given type.
dataType sets the Acceptheader to tell the server that this is the type of response we want e.g.

contentType 设置ContentTypeHTTP 请求头,告诉服务器这个请求的主体是给定的类型。
dataType 设置Accept标头以告诉服务器这是我们想要的响应类型,例如

Accept:application/json, text/javascript, */*; q=0.01

but regardless of what type of response the server sends jQuery will still attempt to parse it as whatever type you set in the dataType field.

但无论服务器发送什么类型的响应,jQuery 仍会尝试将其解析为您在 dataType 字段中设置的任何类型。

回答by Sam Dufel

"application/json" is the correct mime type for json. The jquery dataTypefield, however, is expecting one of the following strings:

“application/json”是 json 的正确 mime 类型。dataType但是,jquery字段需要以下字符串之一:

"xml"
"html"
"script"
"json"
"jsonp"

回答by jchapa

Per the json documentation, the correct dataType is "json".

根据 json 文档,正确的数据类型是“json”。

http://api.jquery.com/jQuery.ajax/

http://api.jquery.com/jQuery.ajax/

Here are the options supported:

以下是支持的选项:

  • xml
  • html
  • script
  • json
  • jsonp
  • text
  • xml
  • html
  • 脚本
  • json
  • jsonp
  • 文本