当响应是text / html时,jQuery $ .getJSON()方法是否会失败?

时间:2016-09-22 15:31:05

标签: javascript jquery json content-type getjson

当我们尝试使用它(不是$.getJSON())时,是否可以强制application/json函数引发错误。似乎这个延迟只是将空对象返回到(或完成)。

我们怎样才能在then处理程序中找到content-type处理过的响应? 我知道当服务不可用时,服务器可以返回HTML而不是JSON。

1 个答案:

答案 0 :(得分:1)

响应为$.getJSON()时,

text/html失败。见这个例子:

$.getJSON('http://httpbin.org/html')
  .then(() => console.log('success'), () => console.log('error'))
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>