Javascript:如何判断 AJAX 响应是否为 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7288136/
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
Javascript: How to tell whether AJAX response is JSON
提问by jawns317
I've got an AJAX request that expects JSON in response.
我有一个 AJAX 请求,需要 JSON 作为响应。
But there's a possibility that what gets returns may not be JSON, but rather an HTML error page (unfortunately, with response type 200).
但是有可能返回的内容可能不是 JSON,而是 HTML 错误页面(不幸的是,响应类型为 200)。
How can I tell whether the response is JSON or not?
如何判断响应是否为 JSON?
(I'm using jQuery, if that helps. But I can't use any plugins.)
(我正在使用 jQuery,如果有帮助的话。但我不能使用任何插件。)
回答by Chad
Well, if you are using jQuery and you specify the dataType
property of the $.ajax()
call to json
then jQuery will try to parse the JSON, and if it isn't JSON should call the error()
callback.
好吧,如果您使用 jQuery 并指定调用的dataType
属性,那么 jQuery 将尝试解析 JSON,如果不是 JSON,则应调用回调。$.ajax()
json
error()
$.ajax({
url: '/my/script.ext',
dataType: 'json',
success: function(data, textStatus, jqXHR) { /*YAYE!!*/ },
error: function(jqXHR, textStatus, errorThrown) { /*AWWW... JSON parse error*/ }
});
EDIT
编辑
For anyone not using jQuery that lands here, the basic idea is to try and parse it as json and catch the error:
对于不使用 jQuery 的人来说,基本思想是尝试将其解析为 json 并捕获错误:
var data = 'some_data';
try {
data = JSON.parse(data);
} catch(e) {
//JSON parse error, this is not json (or JSON isn't in your browser)
}
//act here on the the parsed object in `data` (so it was json).
回答by Arnaud Le Blanc
jQuery auto-detects the dataType:
jQuery 自动检测数据类型:
If the response is JSON, a properly behaving application would set the Content-Type
to application/json.
如果响应是 JSON,则行为正常的应用程序会将 设置Content-Type
为application/json。
So all you have to do, if the server is well-behaving, is to test if the Content-Type header in the response starts with application/json.
因此,如果服务器运行良好,您所要做的就是测试响应中的 Content-Type 标头是否以 application/json 开头。
By chance, jQuery already does this by itself:
碰巧的是,jQuery 已经自己做到了:
$.get('/foo', function(data, status, xhr, dataType) {
if ('json' === dataType) {
// Yay that's JSON !
// Yay jQuery has already parsed `data`
}
});
jQuery detects the dataType and passes it as 4th parameter of the callback function. If the dataType is JSON, it parsed the JSON string and parses the resulting value as first parameter of the callback.
jQuery 检测 dataType 并将其作为回调函数的第四个参数传递。如果 dataType 是 JSON,它会解析 JSON 字符串并将结果值解析为回调的第一个参数。
回答by Joe
Seems like a good use of try catch:
似乎很好地使用了 try catch:
try {
$.parseJSON(input)
} catch(e) {
// not valid JSON
}
回答by Tarik
jQuery parseJSON function can be used for this. It will throw an exception then you can catch it go ahead.
jQuery parseJSON 函数可用于此目的。它会抛出一个异常,然后你可以继续捕捉它。
data = '{}';
try {
json = $.parseJSON(data);
} catch (e) {
// not json
}
回答by Marc B
try {
jQuery.parseJson(json_string_here);
} catch(e) {
... malformed json ...
}