Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我在API天气应用程序的AJAX代码中做错了什么?它赢了';t从站点中提取数据_Javascript_Ajax - Fatal编程技术网

Javascript 我在API天气应用程序的AJAX代码中做错了什么?它赢了';t从站点中提取数据

Javascript 我在API天气应用程序的AJAX代码中做错了什么?它赢了';t从站点中提取数据,javascript,ajax,Javascript,Ajax,我有这个作为我的ajax代码,我试图从openweathermap获取天气数据 $("#sl").click(function() { var city = $("#cityName").val(); console.log(city); getID(city); }); function getID(searchdata) { $.ajax({ url: "http://api.openweathermap.org/data/2.5/weather? q="+cityName+"

我有这个作为我的ajax代码,我试图从openweathermap获取天气数据

$("#sl").click(function() {
var city = $("#cityName").val();
console.log(city);
getID(city);

});

function getID(searchdata) {
$.ajax({
    url: "http://api.openweathermap.org/data/2.5/weather? 
q="+cityName+",ca&&appid=76e0a92437b70a4f70ae9ddc7132491e",
    dataType: "jsonp",
    data: {
      search: searchdata
    },
    success: function(cityID) {
        getinf(cityID);
        console.log(cityID);
    }
})
} 
此外,这是我的html代码,用户在其中搜索城市

<form method="get">
     <div class="row col s10 offset-s1" id="citiesArea" style="display:inline">
          <div class="input-field col s6">
              <input id="cityName" name="cityName" type="text" class="validate">
                <label for="cityName">City Name</label>
          </div>
      </div>
 </form>

城市名称
当我在google chrome上检查元素和控制台时,会看到一条“GET net::ERR_ABORTED”消息。我不知道该怎么做,也不知道如何修复它。我正在创建一个简单的天气应用程序,用户可以使用openweathermap的API搜索城市的天气


谢谢。

一个问题是
q=“+cityName+”,ca&&appid=
中的
&
-将其更改为
&

data:{search:searchdata}
看起来也是多余的(实际上被忽略了)

将函数重写为

函数getID(searchdata){
$.ajax({
url://api.openweathermap.org/data/2.5/weather?q=“+searchdata+”,ca&appid=76e0a92437b70a4f70ae9ddc7132491e”,
数据类型:“json”,
成功:功能(cityID){
console.log(cityID);
}
})
}
getID(‘温哥华’)

&&
?尝试使用
&
代替-同时,
getID(searchdata)
但是
q=“+cityName+”
-确定参数/变量名称并坚持使用它注意:您可以使用
json
而不是
jsonp
,因为openweathermap确实发送CORS响应头