【问题标题】:Does $.getJSON(url.params) correctly format a URL?$.getJSON(url.params) 是否正确格式化 URL?
【发布时间】:2023-03-26 05:25:01
【问题描述】:

我正在从 OpenWeatherAPI 访问 JSON 数据。 URL 的正确格式是

http://api.openweathermap.org/data/2.5/weather?q=Chicago&APPID=34lkj349gga9s8dug9sd8hg

在哪里 ?q={city}&APPID={API_key}

假设我提供了 url、q 参数和 APPID 参数。我使用 $.getJSON 功能来检索 JSON 数据。 $.getJSON 是否知道 URL 是用 ?、= 和 & 格式化的,还是我必须用自己的参数写进去?目前我返回的只是 localhost/?

这是我写的短程序。很好地解释了我期望它如何工作。

  // Here is how the final url should look:
  // api.openweathermap.org/data/2.5/weather?q=Chicago&APPID=33lkr3jlfj39asdflk

var weatherSearch = '';
  // weather-search is my html form id. On submit, send the input
  // (which is city name) to the function getWeather.
$('#weather-search').submit(function(event) {
weatherSearch = $('#weatherQuery').val();
event.preventDefault();
getWeather(weatherSearch);
});

  // getWeather has params q (city name), and APPID (API key).
function getWeather(weatherSearch) {
var params = {
        q: weatherSearch,
        APPID: '33lkr3jlfj39asdflk'
};
  // This is the url that goes before the params.
url = 'http://api.openweathermap.org/data/2.5/weather/';
  // Request data using url and params above.
  // Does $.getJSON format the url properly?
$.getJSON(url. params, function(data) {
  // Pass JSON data to showWeather function.
        showWeather(data.items);
        console.log(data.items);
});
}

function showWeather(weather) {
  // Show JSON data (weather) in html div id="weatherResults"
$('#weatherResults').html(weather);

}

这是 JavaScript 引用的 html。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>weather</title>
<script src="/jquery.js"></script>
<script src="openweather.js"></script>
</head>
<body>

<form id="weather-search">
<input type="text" id="weatherQuery"></input>
<input type="submit" value="Submit"></input>
</form>

<div id="weatherResults">
</div>

【问题讨论】:

    标签: javascript json


    【解决方案1】:

    尝试用逗号“,”代替点“.”:

    $.getJSON(url, params, function(data) {
      // Pass JSON data to showWeather function.
            showWeather(data.items);
            console.log(data.items);
    });
    

    【讨论】:

      【解决方案2】:

      是的,它会自动使用 ?q=weatherSearch&APPID=33lkr3jlfj39asdflk 对 URL 进行编码

      只需将您的 getJson 切换到

      $.getJSON(url, params, function(data) {
        // Pass JSON data to showWeather function.
              showWeather(data.items);
              console.log(data.items);
      });
      

      url 后面有一个句号而不是逗号

      通过工作示例查看此代码笔 http://codepen.io/anon/pen/jqwPyQ

      【讨论】:

      • 是的,但是 javascript 中的连接字符串不是 . 而是 + -> url. params 实际上是 url+params
      • 他无法检索结果,因为它需要 POST 方法。
      • API 抛出错误Object {error: "Please use POST request"} 以任何方式将POST 方法添加到$.getJSON
      • 其实@KalpeshSingh 我看了他们的API,其实是一个GET请求。如果你花点时间看看我的 codepen,你会发现它运行良好
      • @KalpeshSingh 他在 url 后面有一个句号。必须有逗号
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      • 2012-09-02
      • 2011-07-09
      • 1970-01-01
      相关资源
      最近更新 更多