简体   繁体   English

在 HTTPS 页面中运行 HTTP AJAX 操作时出现“混合内容被阻止”

[英]"Mixed content blocked" when running an HTTP AJAX operation in an HTTPS page

I've a form which I'm submitting (through GET as it is required this way) to a crm (ViciDial).我有一个要提交给 crm (ViciDial) 的表格(通过 GET,因为它需要这种方式)。 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.我可以成功提交表单,但是如果我这样做,crm 上的处理文件将只回显成功文本,仅此而已。

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:我想在我的网站上显示感谢页面而不是该文本,因此我决定使用 AJAX 提交表单并将其重定向到我需要的页面,但是我的浏览器出现此错误:

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'.混合内容:“https://page.com”的页面已通过 HTTPS 加载,但请求了不安全的 XMLHttpRequest 端点“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.内容必须通过 HTTPS 提供。

This is my AJAX script:这是我的 AJAX 脚本:

<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>

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?仅在 URL 中设置 https 是行不通的,有什么方法可以通过 GET 提交数据并将用户重定向到我的谢谢页面?

============================ ============================

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.这里的问题是混合内容,这意味着我通过 HTTPS 加载了一个页面,并试图通过 AJAX 和 HTTP 中的 API 进行访问。但是浏览器不允许我们这样做。

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 设置为 HTTPS(这是我的情况),我们仍然可以采用不同的方式来解决这个问题。

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.主要问题不是混合内容问题,而是我想将数据提交到 API 并将用户重定向到花哨的感谢页面。 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.我没有使用 AJAX,而是制作了一个 php 文件,该文件接收数据,使用 curl 将其发送到 API(因为这是在服务器端完成的,所以没有混合内容问题),并将我的快乐用户重定向到一个精美的感谢页面。

I resolved this by adding following code to the HTML page, since we are using the third party API which is not controlled by us.我通过向 HTML 页面添加以下代码解决了这个问题,因为我们使用的是不受我们控制的第三方 API。

<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests"> 

Hope this would help, and for a record as well.希望这会有所帮助,并为记录。

If you load a page in your browser using HTTPS, the browser will refuse to load any resources over HTTP.如果您使用 HTTPS 在浏览器中加载页面,浏览器将拒绝通过 HTTP 加载任何资源。 As you've tried, changing the API URL to have HTTPS instead of HTTP typically resolves this issue .正如您所尝试的那样,将 API URL 更改为使用 HTTPS 而不是 HTTP 通常可以解决此问题 However, your API must not allow for HTTPS connections.但是,您的 API 不得允许 HTTPS 连接。 Because of this, you must either force HTTP on the main page or request that they allow HTTPS connections.因此,您必须在主页上强制使用 HTTP 或请求它们允许 HTTPS 连接。

Note on this: The request will still work if you go to the API URL instead of attempting to load it with AJAX.请注意:如果您转到 API URL 而不是尝试使用 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.但是,为了通过 AJAX 使用它,协议应该匹配。

If you are just visiting a webpage that you trust and you want to move forward fast, just:如果您只是在访问您信任的网页并且想要快速前进,只需:

1- Click the shield icon in the far right of the address bar. 1- 单击地址栏最右侧的盾牌图标。

允许在 Google Chrome 中混合内容

2- In the pop-up window, click "Load anyway" or "Load unsafe script" (depending on your Chrome version). 2- 在弹出窗口中,单击“仍然加载”或“加载不安全的脚本”(取决于您的 Chrome 版本)。


If you want to set your Chrome browser to ALWAYS(in all webpages) allow mixed content:如果要将 Chrome 浏览器设置为始终(在所有网页中)允许混合内容:

1- In an open Chrome browser, press Ctrl+Shift+Q on your keyboard to force close Chrome. 1- 在打开的 Chrome 浏览器中,按键盘上的 Ctrl+Shift+Q 强制关闭 Chrome。 Chrome must be fully closed before the next steps.在下一步之前必须完全关闭 Chrome。

2- Right-click the Google Chrome desktop icon (or Start Menu link). 2- 右键单击​​ Google Chrome 桌面图标(或开始菜单链接)。 Select Properties.选择属性。

3- At the end of the existing information in the Target field, add: " --allow-running-insecure-content" (There is a space before the first dash.) 3- 在目标字段中现有信息的末尾,添加:“--allow-running-insecure-content”(第一个破折号前有一个空格。)

4- Click OK. 4- 单击确定。

5- Open Chrome and try to launch the content that was blocked earlier. 5- 打开 Chrome 并尝试启动之前被阻止的内容。 It should work now.它现在应该可以工作了。

The reason for this error is very simple.这个错误的原因很简单。 Your AJAX is trying to call over HTTP whereas your server is running over HTTPS, so your server is denying calling your AJAX.您的 AJAX 试图通过 HTTP 调用,而您的服务器通过 HTTPS 运行,因此您的服务器拒绝调用您的 AJAX。 This can be fixed by adding the following line inside the head tag of your main HTML file:这可以通过在主 HTML 文件的 head 标记中添加以下行来解决:

<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests"> 

Mikel's answer above is correct;上面 Mikel 的回答是正确的; it's related to the frontend with HTTPS and the backend with HTTP;它与HTTPS的前端和HTTP的后端相关; So install the SSL certificate on your backend.因此,在您的后端安装 SSL 证书。

To workaround that during testing on my chrome browser;在我的 chrome 浏览器上测试期间解决这个问题; I changed the chrome settings for the desired site to insecure content as the pictures below我将所需站点的 chrome 设置更改为不安全的内容,如下图所示

1- click on the locker icon and choose site settings 1- 单击储物柜图标并选择站点设置

在此处输入图像描述

2- At the insecure content change it from block to allow. 2- 在不安全的内容中将其从阻止更改为允许。

在此处输入图像描述

I had the same issue with Axios requests in a Vue-CLI App.我在 Vue-CLI 应用程序中遇到了与 Axios 请求相同的问题。 On further checking in Network tab of Chrome, I found that the request URL of axios correctly had https but response 'location' header was http .在 Chrome 的网络选项卡中进一步检查时,我发现 axios 的请求 URL 正确具有https但响应 'location' 标头是http

This was caused by nginx and I fixed it by adding these 2 lines to server config for nginx:这是由nginx引起的,我通过将这两行添加到 nginx 的服务器配置来修复它:

server {
    ...
    add_header Strict-Transport-Security "max-age=31536000" always;
    add_header Content-Security-Policy upgrade-insecure-requests;
    ...
}

Might be irrelevant but my Vue-CLI App was served under a subpath in nginx with config as:可能无关紧要,但我的 Vue-CLI 应用程序是在 nginx 的子路径下提供的,配置如下:

location ^~ /admin {
        alias   /home/user/apps/app_admin/dist;
        index  index.html;
        try_files $uri $uri/ /index.html;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Host $host;
}

If your API code is running on a node.js server then you need to focus your attention there, not in Apache or NGINX.如果您的 API 代码在node.js服务器上运行,那么您需要将注意力集中在那里,而不是在 Apache 或 NGINX 中。 Mikel is right, changing the API URL to HTTPS is the answer but if your API is calling a node.js server, it better be set up for HTTPS! Mikel 是对的,将 API URL 更改为 HTTPS 是答案,但是如果您的 API 正在调用 node.js 服务器,最好设置为 HTTPS! And of course, the node.js server can be on any unused port, it doesn't have to be port 443.当然,node.js 服务器可以在任何未使用的端口上,不必是端口 443。

Instead of using Ajax Post method, you can use dynamic form along with element.您可以将动态表单与元素一起使用,而不是使用 Ajax Post 方法。 It will works even page is loaded in SSL and submitted source is non SSL.即使页面在 SSL 中加载并且提交的源是非 SSL,它也能工作。

You need to set value value of element of form.您需要设置表单元素的值。

Actually new dynamic form will open as non SSL mode in separate tab of Browser when target attribute has set '_blank'实际上,当目标属性设置为“_blank”时,新的动态表单将在浏览器的单独选项卡中以非 SSL 模式打开

var f = document.createElement('form');
f.action='http://XX.XXX.XX.XX/vicidial/non_agent_api.php';
f.method='POST';
//f.target='_blank';
//f.enctype="multipart/form-data"

var k=document.createElement('input');
k.type='hidden';k.name='CustomerID';
k.value='7299';
f.appendChild(k);



//var z=document.getElementById("FileNameId")
//z.setAttribute("name", "IDProof");
//z.setAttribute("id", "IDProof");
//f.appendChild(z);

document.body.appendChild(f);
f.submit()

in my case, my localhost was http and my deployed version was https , so i used this script to add http-equiv meta tag only for https:就我而言,我的 localhost 是http而我部署的版本是https ,所以我使用这个脚本为 https 添加 http-equiv 元标记:

if (window.location.protocol.indexOf('https') == 0){
  var el = document.createElement('meta')
  el.setAttribute('http-equiv', 'Content-Security-Policy')
  el.setAttribute('content', 'upgrade-insecure-requests')
  document.head.append(el)
}

I was same problem, but for me the problem was ng build command.我是同样的问题,但对我来说问题是 ng build 命令。 I was doing "ng build --prod" i have corrected it to "ng build --prod --base-href /applicationname/".我正在做“ng build --prod”,我已将其更正为“ng build --prod --base-href /applicationname/”。 and this solved my problem.这解决了我的问题。

This solution work for me fine这个解决方案对我很好

just add to the head只需添加到头部

<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests"> 

I want to share a slight oddity which triggered a Mixed Content issue for me.我想分享一个对我来说触发混合内容问题的小怪事。 I am using DataTables and was providing an AJAX url which looked like this:我正在使用 DataTables 并提供一个 AJAX url 看起来像这样:

mypath/ajax_get/'.$clientID

This was set in PHP but $clientID wasn't always set.这是在 PHP 中设置的,但并不总是设置$clientID As such when the Datatables code ran it simply ended ajax_get/ .因此,当 Datatables 代码运行时,它只是结束了ajax_get/ For some unknown reason, this was enough to bypass https and try and load over http instead.由于某些未知原因,这足以绕过 https 并尝试加载 http。 I don't think this is a browser bug as I tried it in Chrome and Firefox but if you're having problems, I hope this helps.我不认为这是一个浏览器错误,因为我在 Chrome 和 Firefox 中尝试过,但如果您遇到问题,我希望这会有所帮助。

As always with html/debugging, if in doubt, create a duplicate page and strip chunks of code back until you can pinpoint the issue!与 html/调试一样,如果有疑问,请创建一个重复页面并剥离代码块,直到您可以查明问题所在!

For laravel 8 in local it was ok but in production I had the problem.对于本地的 laravel 8 没问题,但在生产中我遇到了问题。 To solve it I used POST method and removed a simple slash at final of url.为了解决这个问题,我使用了 POST 方法并在 url 的末尾删除了一个简单的斜杠。 I changed it from:我把它从:

/my/url/ /我的/网址/

to:到:

/my/url /我的/网址

and it works.它有效。

I don't know the reason.我不知道原因。 Perhaps somebody could explain it.也许有人可以解释一下。

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

相关问题 未阻止从 HTTPS 页面到 HTTP(非 HTTPS)本地主机地址的混合内容请求 - Mixed-content request from HTTPS page to HTTP (non-HTTPS) localhost address not blocked 在HTTPS页面中访问HTTP时出现“混合内容”错误 - “Mixed content” Error when accessing HTTP within HTTPS page 混合内容 jQuery ajax HTTPS 请求已在 Laravel 上被阻止 - Mixed content jQuery ajax HTTPS request has been blocked on Laravel 如何修复受阻止的混合内容的Drupal(https)网站 - How to fix a drupal (https) website with blocked mixed content HTTPS端点请求返回为混合内容HTTP - HTTPS endpoint request returns as Mixed Content HTTP Heroku混合内容HTTPS / HTTP问题 - Heroku Mixed content HTTPS/HTTP issue 添加 HTTP 链接到 SSL HTTPS 页面,而不会导致混合内容“不安全”错误 - Add a HTTP link to SSL HTTPS page without causing Mixed-Content “Not Secure” error 已阻止通过安全连接将地理位置混合访问到https:// localhost:3000,无法访问地理位置 - blocked Access to geolocation was blocked over secure connection with mixed content to https://localhost:3000 将HTTP框架嵌入HTTPS父级时防止混合内容错误 - Prevent mixed content error when embedding HTTP frame inside an HTTPS parent 将iframe中的页面从https加载到https时出现混合内容错误 - Errors of mixed content while loading page in iframe from https to https
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM