“混合内容被阻止"在 HTTPS 页面中运行 HTTP AJAX 操作时 [英] "Mixed content blocked" when running an HTTP AJAX operation in an HTTPS page

查看:32
本文介绍了“混合内容被阻止"在 HTTPS 页面中运行 HTTP AJAX 操作时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,我正在向 crm (ViciDial) 提交(通过 GET,因为它需要这种方式).我可以成功提交表单,但是如果我这样做,crm 中的处理文件只会回显成功文本,仅此而已.

I've a form which I'm submitting (through GET as it is required this way) to a crm (ViciDial). I can successfully submit the form however if I do that the processing file at crm will just echo a success text and that's it.

我想在我的网站上显示一个感谢页面而不是那个文本,所以我决定使用 AJAX 提交表单并将其重定向到我需要的页面,但是我在浏览器上收到此错误:

Instead of that text, I want to display a thank-you page on my website, so I decided to use AJAX to submit the form and redirect it to the page I need, however I'm getting this error on my browser:

混合内容:位于https://page.com"的页面是通过 HTTPS 加载的,但请求了一个不安全的 XMLHttpRequest 端点'http://XX.XXX.XX.XXX/vicidial/non_agent_api.php?queries=query=data'.此请求已被阻止;内容必须通过 HTTPS 提供.

Mixed Content: The page at 'https://page.com' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'http://XX.XXX.XX.XXX/vicidial/non_agent_api.php?queries=query=data'. This request has been blocked; the content must be served over HTTPS.

这是我的 AJAX 脚本:

This is my AJAX script:

    <script>
    SubmitFormClickToCall = function(){

        jQuery.ajax({
            url: "http://XX.XXX.XX.XX/vicidial/non_agent_api.php",
            data : jQuery("#form-click-to-call").serialize(),
            type : "GET",
            processData: false,
            contentType: false,
            success: function(data){
                window.location.href = "https://www.example.com/thank-you";
            }
        });
    }
    </script>

仅在 URL 中设置 https 不起作用,有什么方法可以通过 GET 提交数据并将用户重定向到我的感谢页面?

Just setting https in the URL won't work, is there any way in which I can submit the data via GET and redirect the user to my thankyou page?

这里的问题是混合内容,这意味着我通过 HTTPS 加载了一个页面,并试图通过 AJAX 访问 HTTP 中的 API.但是浏览器不允许我们这样做.

Problem here was mixed content, this means that I loaded a page through HTTPS and was trying to hit via AJAX an API that was in HTTP. But the browser wont allow us to just do that.

因此,如果您无法将 API 设置为 HTTPS(这是我的情况),我们仍然可以采用不同的方式来解决此问题.

So if you can't set the API to be HTTPS (this was my case) we can still approach this in a different way.

主要问题不是混合内容问题,而是我想将数据提交到 API 并将用户重定向到一个漂亮的感谢页面.我没有使用 AJAX,而是制作了一个接收数据的 php 文件,使用 curl 将其发送到 API(因为这是在服务器端完成的,没有混合内容问题)并将我满意的用户重定向到一个漂亮的感谢页面.

The Main Problem was not the mixed content issue it was that I wanted to submit data to an API and redirect the users to a fancy thank you page. Instead of using AJAX, i made a php file that receives the data sends it using curl to the API (as this is being done server side there is no mixed content issue) and redirects my happy user to a fancy thank you page.

推荐答案

如果您在浏览器中使用 HTTPS 加载页面,浏览器将拒绝通过 HTTP 加载任何资源.正如您所尝试的那样,更改 API URL 以使用 HTTPS 而不是 HTTP 通常可以解决此问题.但是,您的 API 不得允许 HTTPS 连接.因此,您必须在主页面上强制使用 HTTP 或请求它们允许 HTTPS 连接.

If you load a page in your browser using HTTPS, the browser will refuse to load any resources over HTTP. As you've tried, changing the API URL to have HTTPS instead of HTTP typically resolves this issue. However, your API must not allow for HTTPS connections. Because of this, you must either force HTTP on the main page or request that they allow HTTPS connections.

请注意:如果您转到 API URL 而不是尝试使用 AJAX 加载它,请求仍然有效.这是因为浏览器不是从安全页面加载资源,而是加载不安全页面并接受它.但是,为了通过 AJAX 使用它,协议应该匹配.

Note on this: The request will still work if you go to the API URL instead of attempting to load it with AJAX. This is because the browser is not loading a resource from within a secured page, instead it's loading an insecure page and it's accepting that. In order for it to be available through AJAX, though, the protocols should match.

这篇关于“混合内容被阻止"在 HTTPS 页面中运行 HTTP AJAX 操作时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆