如何使用jQuery制作AJAX HTTPS GET请求 [英] How to Make an AJAX HTTPS GET Request Using jQuery

查看:152
本文介绍了如何使用jQuery制作AJAX HTTPS GET请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用jQuery明确地创建AJAX HTTPS GET请求?我正在尝试做以下事情。
在https页面上,我有一行代码 $。get(/ resource),但是我得到以下错误:

  XMLHttpRequest无法加载http://www.site.com/resource。请求的资源上不存在Access-Control-Allow-Origin标头。原因'https://www.site.com'因此不允许访问。 

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



jQuery版本是1.7.2 解决方案

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



在总之,我将 $。get(/ resource)替换为 $。get(/ resource /)



谢谢。我非常感谢您的帮助。


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

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-domain could not resolve it.

jQuery Version is 1.7.2

解决方案

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 redirect to the URL with the trailing slash using an HTTP request instead of an HTTPS request.

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

Thank you. I really appreciate all of your help.

这篇关于如何使用jQuery制作AJAX HTTPS GET请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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