JavaScript的跨域调用:从HTTP调用到HTTPSJavaScript、HTTP、HTTPS

2023-09-07 11:07:38 作者:天青色等烟云

我需要做一个异步调用到安全(HTTPS)的URL相同的域。

I need to make an asynchronous call to a secure (HTTPS) URL for the same domain.

目前该页面正在与普通的HTTP(非安全)。

Currently the page is working with regular HTTP (non-secure).

在换句话说:这是调用同一个域中的网址,但使用HTTPS

In other words: this is calling an URL in the same domain but using HTTPS.

在此切换调用到HTTPS我最终实现一个服务器端的代理,允许跨域AJAX调用,但我现在由于HTTP面临着同样的原产地政策和HTTPS被认为是不同的起源了。所以,这个代理是无法使用的。

Before switching this calls to HTTPS I ended implementing a server-side proxy to allow cross-domain AJAX calls, but now I'm facing same origin policy since HTTP and HTTPS are considered different origins too. So this proxy is unusable.

摘要:如何做跨域,在这种情况下asnynchronous POST请求

各种注意事项:

在我不能接受任何回答提示JSONP。异步调用必须使用POST动词。 在我使用jQuery的最新版本。答案可以基于此库,或任何其它解决此问题。 访问整个页面通过HTTPS不是一个解决方案。 在服务器平台是微软.NET 4.0(ASP.NET 4.0)。 UDPATE :CORS是不是一种选择。有这个在现代浏览器没有广泛的支持。 I couldn't accept any answer suggesting JSONP. Asynchronous calls must be using POST verb. I'm using latest version of jQuery. Answer could be based on this library, or any other solving this problem. Accessing the entire page over HTTPS isn't a solution. Server platform is Microsoft .NET 4.0 (ASP.NET 4.0). UDPATE: CORS isn't an option. There's no wide support for this in modern browsers.

推荐答案

首先,我有一从@missingo和@PiTheNumber这两个问题。

First of all, I've +1 both questions from @missingo and @PiTheNumber.

花了很多小时后,我到了这样的结论我要整个页面切换到HTTPS。这是因为:

After spending a lot of hours, I've arrived to the conclusion I'm going to switch the entire page to HTTPS. That's because:

大多数现代人浏览器都支持CORS,但IE浏览器,从第8版本开始有一个专有的实现(XDomainRequest对象),这可能会在某些计算机被禁用(我已经跨域请求,默认情况下禁用在Internet安全区域)。

Most moderns browsers support CORS, but Internet Explorer, starting from 8th version has a proprietary implementation (XDomainRequest object), which may be disabled in some computers (mine had cross-domain request disabled by default in Internet security zone).

Opera不支持CORS。 12版本将支持它,但是这不是一个选择,因为用户首先应该采用这种新版本,这不会是在2天。

Opera doesn't support CORS. 12th version will support it, but this isn't an option as users should adopt this new version first, and this won't be in 2 days.

我需要做跨域请求,因为Web客户端应用程序必须请求位于另一域中的RESTful服务层。没办法。

I need to do cross-domain requests since Web client application must request a RESTful service layer located in another domain. No way.

切换一切HTTPS使服务层代理方式再次工作(这是预期的行为)。

Switching everything to HTTPS makes the service layer proxy approach work again (this is the expected behavior).

还是要谢谢你,因为这两个答案都帮了我很多的到达了这样的结论。

Thanks anyway because both answer have helped me a lot for arriving to this conclusion.

@Sam又增加了一个评论,可能是有趣的人。它是关于如何让在Internet Explorer 8 CORS和9(见#7):http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx

@Sam has added a comment that could be interesting for anyone. It's about how to get CORS in Internet Explorer 8 and 9 (see #7): http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx