简体   繁体   English

为什么这个 jQuery.ajax 不会引发错误?

[英]Why does this jQuery.ajax not raise an error?

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. 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:但是,我的软件中有一个错误,我试图发送以下 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.我知道这会失败 - 我可以接受 jQuery 不想遵循重定向。 But the actual behaviour is even weirder: I never see an xhr request go out at all, And there's no javascript error.但实际行为更奇怪:我根本没有看到 xhr 请求 go 出来,也没有 javascript 错误。 It just fails, silently, If I change the url to https, or to a relative path.如果我将 url 更改为 https 或相对路径,它就会默默地失败。 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?为什么它没有引发错误?

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 <script>标签并将其放入 DOM,并使用该机制加载文件。

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.这工作得相当好(考虑到这是对错误的浏览器“安全”概念的彻底破解),但遗憾的是,jQuery 无法在此时捕获错误。 You will likely get a browser error if you have developer mode turned on, but that's it.如果您打开了开发人员模式,您可能会收到浏览器错误,但仅此而已。

If you run that from an url that's https and try to open the equivalent http page you run into cross domain problems due to the different protocols they use.如果您从 url 即https运行它并尝试打开等效的http页面,由于它们使用的协议不同,您会遇到跨域问题。 Have a look at same origin policy .看看同源政策

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM