gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-27 22:30:22 25 4
gpt4 key购买 nike

当我们尝试使用它获取 HTML(而不是 application/json)时,是否可以强制 $.getJSON() 函数引发错误。看来这个 deferred 只是将空对象返回给 then(或完成)。

我们如何在 then 处理程序中找到响应中处理的 content-type ?我遇到过当服务不可用时服务器可以返回 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>

关于javascript - 当响应是 text/html 时,jQuery $.getJSON() 方法应该失败吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39643181/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com