如何使用 jQuery 发出 AJAX HTTPS GET 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21056990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 01:25:08  来源:igfitidea点击:

How to Make an AJAX HTTPS GET Request Using jQuery

jqueryajaxhttpsget

提问by FearlessFuture

How can I explicitly make an AJAX HTTPS GET request using jQuery? I am trying to do the following. On an https page, I have a line with the code $.get("/resource"), but I get the following error

如何使用 jQuery 显式发出 AJAX HTTPS GET 请求?我正在尝试执行以下操作。在 https 页面上,我有一行代码$.get("/resource"),但出现以下错误

XMLHttpRequest cannot load http://www.site.com/resource. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://www.site.com' is therefore not allowed access.

Why is the AJAX call trying to access the page using the HTTP protocol if the relative resource is from an https page? If the $.get(url) method does this by default, how do I use jQuery to do an explicit HTTPS GET request? Another person, who had a similar issue, at http://forum.jquery.com/topic/jquery-get-ajax-call-on-http-page-to-https-on-same-domaincould not resolve it.

如果相关资源来自 https 页面,为什么 AJAX 调用会尝试使用 HTTP 协议访问页面?如果 $.get(url) 方法默认执行此操作,我如何使用 jQuery 执行显式 HTTPS GET 请求?另一个遇到类似问题的人,在http://forum.jquery.com/topic/jquery-get-ajax-call-on-http-page-to-https-on-same-domain无法解决它。

jQuery Version is 1.7.2

jQuery 版本是 1.7.2

回答by FearlessFuture

I fixed the issue. It turned out that due to the way that our Django site was configured, I needed to add a trailing slash to resource in the AJAX request. Without the trailing the slash, Django would then redirectto the URL with the trailing slash using an HTTP request instead of an HTTPS request.

我解决了这个问题。事实证明,由于我们 Django 站点的配置方式,我需要在 AJAX 请求中的资源中添加一个尾部斜杠。如果没有尾部斜杠,Django 将使用 HTTP 请求而不是 HTTPS 请求重定向到带有尾部斜杠的 URL。

In short, I replaced $.get("/resource")with $.get("/resource/").

简而言之,我$.get("/resource")$.get("/resource/").

Thank you. I really appreciate all of your help.

谢谢你。我真的很感谢你的帮助。

回答by mituw16

If the page you are on is an https page, and the page .get is trying to access is http, then that will not work due to same origin. However, you could just write out the ajax instead of short handing it with .get :)

如果您所在的页面是 https 页面,而 .get 尝试访问的页面是 http,那么由于同源,这将不起作用。但是,您可以只写出 ajax 而不是用 .get 简写:)

$.ajax({
    type: "GET", 
    url: "https://someurl"
});

Though I suppose to be fair, that is still a short of true javascript

虽然我认为是公平的,但这仍然是真正的 javascript

回答by alphapilgrim

Try setting the datatype to "jsonp", that's helped me in the past with cross-origin requests.

尝试将数据类型设置为“jsonp”,这在过去帮助我处理跨域请求。

    $.ajax({
            url: "//www.site.com/resource"
            dataType: "jsonp",
            success: function(data) {
                $(".demo-card").html(data);
            }
    });

回答by Alex S

Just use RewriteRule in your .htaccess file with the specified protocol, for example:

只需在您的 .htaccess 文件中使用指定协议的 RewriteRule 即可,例如:

RewriteCond %{REQUEST_URI} .+[^/]$
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L,QSA]