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

Should jQuery $.getJSON() method fail when the response is text/html?

是否可以强制 $.getJSON() 函数在我们尝试使用它获取 HTML 时引发错误(而不是 application/json)。似乎这只是将 return 的空对象推迟到 then(或 done)。

我们如何在 then 处理程序中找到响应处理的 content-type? 当服务不可用时,我遇到服务器可以 return HTML 而不是 JSON 的情况。

当响应为 text/html 时,

$.getJSON() 失败。看这个例子:

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