为什么这个jQuery。ajax不会引发错误吗?
We had an interesting issue this morning - the details of the issue itself aren't relevant here, and I already fixed it, but I did run into something strange, to me, about jQuery.
今天早上我们遇到了一个有趣的问题——问题本身的细节在这里并不相关,我已经修复了它,但是我确实遇到了一些奇怪的东西,关于jQuery。
The site I am building internally runs on https, only, so Apache is set to redirect any inbound http request to its https equivalent. This redirect is working fine. But, I had a bug in my software where I was trying to send the following ajax request:
我正在构建的站点只在https上运行,因此Apache被设置为将任何入站http请求重定向到它的https对等服务器。这个重定向运行良好。但是,我的软件有一个bug,我想发送以下ajax请求:
jQuery.ajax({ type: "PUT",
url: "http://somewhere.com/cmdt/todo_lists/8457/toggle",
data: { deployment_id: 827},
dataType: "script"});
I understand that this would fail - I'm alright with jQuery not wanting to follow a redirect. But the actual behaviour is even weirder: I never see an xhr request go out at all! And there's no javascript error! It just fails, silently. If I change the url to https, or to a relative path, it works fine, no problem. My question is, why wasn't it TRYING to send out the request before? And why didn't it raise an error?
我理解这将会失败——对于jQuery不希望遵循重定向,我没有意见。但实际的行为更奇怪:我从来没有见过xhr发出任何请求!并且没有javascript错误!它只是失败,默默地。如果我将url更改为https或相对路径,它可以正常工作,没有问题。我的问题是,为什么它之前不尝试发出请求呢?为什么它没有引起误差呢?
2 个解决方案
#1
3
The reason you're not getting a failure is because it's a cross-site request, and so instead of using XMLHttpRequest
, it's actually generating an HTML <script>
tag and dropping it into the DOM, and using that mechanism to load the file.
您没有收到失败的原因是它是一个跨站点请求,因此它不是使用XMLHttpRequest,而是生成一个HTML
This works reasonably well (considering it's a complete hack around wrong-headed browser "security" notions) but there's no way for jQuery to trap errors at that point, sadly. You will likely get a browser error if you have developer mode turned on, but that's it.
这非常有效(考虑到这是对错误的浏览器“安全性”概念的彻底破解),但遗憾的是,jQuery在这一点上没有办法捕捉错误。如果打开了developer mode,可能会出现浏览器错误,但仅此而已。
更多相关文章
- 点击JSON数据加载Galleria画廊。我需要新鲜的眼睛来看我的错误
- 浏览器Bug在输入上显示时间文本
- jQuery:离线后发布错误(iOS和Chrome)
- 【jQuery】判断浏览器类型和版本
- jQuery Ui Draggable在移动端浏览器不起作用解决方案
- 在线请教调用Jquery错误:TypeError: a is undefined 的错误原因
- JS检查浏览器是否支持CSS Snap Points
- 在发出xml Ajax请求时获取错误412
- Internet Explorer导致无效的真实性令牌错误