如何在HTTP(非安全)服务器上使用jQuery GET?(How to use jQuery GET on HTTP (non

编程入门 行业动态 更新时间:2024-10-28 12:30:25
如何在HTTP(非安全)服务器上使用jQuery GET?(How to use jQuery GET on HTTP (non-secure) server?)

当我尝试在非安全站点(即http,而不是https)上使用$.get时,我收到此错误:

jquery.min.js:4混合内容:'...'页面是通过HTTPS加载的,但请求了一个不安全的脚本' http://api.openweathermap.org/data/2.5/weather?lat=50&lon=2 ?callback = jQuery ......'。 此请求已被阻止; 内容必须通过HTTPS提供。

我一直在努力想办法解决这个问题。 问题是固定的,因为服务器由OpenWeather.org托管,而且它是一个非安全的站点(即http,而不是https)。

这是我的请求代码: $.get("https://api.openweathermap.org/data/2.5/weather?lat=" + latitude + "&lon=" + longitude + "&APPID=123456", function(data) { tempC = data.weather.main.temp / 10; // OpenWeather API returns Celsius * 10 rain = data.rain["3h"]; clouds = data.clouds.all; });

当然,简单地将请求URL更改为https://api.openweathermap.org ...不起作用。 尝试过并且没有用。

我现在能想到的唯一解决方案是找到可以免费用于我的项目的另一个天气API,但我想知道是否还有一种方法可以使用OpenWeathermap的API,因为它是http。 很想知道这一点,因为仅仅因为它是http而不是https而忽略某些API似乎很浪费。

I'm getting this error when I try to use $.get on a non-secure site (ie. http, not https):

jquery.min.js:4 Mixed Content: The page at '...' was loaded over HTTPS, but requested an insecure script 'http://api.openweathermap.org/data/2.5/weather?lat=50&lon=2?callback=jQuery...'. This request has been blocked; the content must be served over HTTPS.

I've been trying to think of work-around solutions to this. The problem is a fixed one, since the server is hosted by OpenWeather.org and it's a non-secure site (ie. http, not https).

This is my request code: $.get("https://api.openweathermap.org/data/2.5/weather?lat=" + latitude + "&lon=" + longitude + "&APPID=123456", function(data) { tempC = data.weather.main.temp / 10; // OpenWeather API returns Celsius * 10 rain = data.rain["3h"]; clouds = data.clouds.all; });

Simply changing the request URL to https://api.openweathermap.org... doesn't work, of course. Tried it and didn't work.

The only solution I can think of right now is to find another weather API that is free to use for my project, but I'd like to know if there's a way to still use OpenWeathermap's API, given that it's http. Curious to know this because it seems quite wasteful to have to dismiss certain APIs just because it's http and not https.

最满意答案

所以,问题是,您在HTTPS站点(JSFiddle和Coodepen)上运行代码。 出于安全原因,您的浏览器不允许在HTTPS站点上使用HTTP-Connections。 您可以通过在运行代码的页面上强制HTTP(尝试从本地文件或本地主机运行代码)来解决该问题,或者您可以在服务器上创建HTTPS - > HTTP转发,它将接收HTTPS请求从您的代码中发送HTTP请求到API。

我建议首先尝试从本地主机或本地文件运行(不确定每个浏览器是否允许从本地文件中使用AJAX,但您可以在设置localhost之前尝试),这应该适合您。 如果您只想测试API,可以简单地将GET请求的URL复制到浏览器选项卡中并执行它。

so, the Problem is, that you run your code on a HTTPS site(JSFiddle and Coodepen). Your browser will not allow HTTP-Connections on a HTTPS site for security-reasons. You can solve that issue by either forcing HTTP on the page where you run your code(try to run a code from a local file or localhost) or you could create a HTTPS -> HTTP forwarding on your server, that would receive a HTTPS request from your code and send a HTTP-request to API.

I would suggest first try to run from a localhost or local file(not sure if every browser will allow AJAX from a local file, but you can try before setting up localhost), that should work for you. If you just want to test the API you can simple copy the URL of the GET-request into you browser tab and execute it.

更多推荐

http,API,https,org,电脑培训,计算机培训,IT培训"/> <meta name="descriptio

本文发布于:2023-07-05 02:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1032135.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:器上   如何在   HTTP   jQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!