为什么Ajax函数在按钮单击事件上没有响应

问题描述 投票:0回答:1

$(document).ready(function(){

            $.ajax({
                url: "https://api.openweathermap.org/data/2.5/weather?q=London,uk&APPID=09e7a4e8ee418a607b4fcec06929900e",
                async: true,
                type: "GET",
                datatype:"json",
                success: function (data) {
                    $("#weather").text(Math.floor(data.main.temp - 273.15) + decodeURI('%C2%B0') + "C")///Add the weather from api
                    $("#location").text(data.name);
                    $('#Status').text(data.weather[0].main)
                    if (data.weather[0].icon == "10n") {
                        var imgurl = "/Images/showerrain.jpg"
                        $('#Weather-Header').css('background-image', 'url(https://media.giphy.com/media/t7Qb8655Z1VfBGr5XB/giphy.gif)', 'background-size', 'cover','background-repeat','no-repeat');
                    }
                    console.log(data)
                },
                error: function (ex) {

                    console.log("Error");
                }

            });

        });

[当我通过document.ajax调用它时,它可以正常工作,当我在按钮单击事件中调用相同的ajax时,即使出现错误也无法响应,请帮忙,在我的按钮单击事件代码下面查找$(document).ready(function(){

      $("#Searchbut").click('click', function () {

          $.ajax({
              url: "https://api.openweathermap.org/data/2.5/weather?q=Chennai&APPID=09e7a4e8ee418a607b4fcec06929900e",
              async: true,
              type: "GET",
              datatype: "json",
              success: function (data) {
                  $("#weather").text(Math.floor(data.main.temp - 273.15) + decodeURI('%C2%B0') + "C")///Add the weather from api
                  $("#location").text(data.name);
                  $('#Status').text(data.weather[0].main)
                  if (data.weather[0].icon == "10n") {
                      var imgurl = "/Images/showerrain.jpg"
                      $('#Weather-Header').css('background-image', 'url(https://media.giphy.com/media/t7Qb8655Z1VfBGr5XB/giphy.gif)', 'background-size', 'cover', 'background-repeat', 'no-repeat');
                  }
                  alert(data);
              },
              error: function (ex) {

                  console.log("Error");
              }

          });
      });
jquery ajax
1个回答
0
投票

传递给click函数的参数不正确。 click()方法触发click事件,或附加一个函数以在单击事件发生时运行。

© www.soinside.com 2019 - 2024. All rights reserved.