我正在使用 jQuery 的 ajax 方法与 Twitter REST API 进行交互.
他们的 API 有点烦人,因为某些操作会返回 401 HTTP 状态代码。在那种情况下,我只是希望它失败,而不是显示登录框。
我试过在 URL 中提供一个不正确的 user:pass 对,以防这可能会抑制对话框并给我一个错误响应,但这并没有帮助。
This question问了完全相同的问题,但答案却是首先解决了如何不触发 401。
(特别是这很烦人,因为这是他们请求身份验证的错误——users/show
方法一开始就不应该请求它。)
最佳答案
查看 suppress_response_codes
在页面下方大约 1/4 处进行了解释: https://dev.twitter.com/docs/things-every-developer-should-know
来自 Twitter 文档(以防再次移动):
suppress_response_codes: If this parameter is present, all responses will be returned with a 200 OK status code - even errors. This parameter exists to accommodate Flash and JavaScript applications running in browsers that intercept all non-200 responses. If used, it's then the job of the client to determine error states by parsing the response body. Use with caution, as those error messages may change.
关于javascript - 在 Twitter 上使用 XmlHttpRequest 时,我可以在 401 响应时抑制浏览器的登录提示吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/426834/