How to use js to create an error message that is displayed on the user interface?

771 Views Asked by At

I have created a table that displays information from Geonames API. I am now trying to set up a function for catching an error when the user leaves the input fields empty and sends an alert to the interface. This is my code below. I'm struggling to understand how I do this.

    $("#btnWeatherRun").click(function () { 
     $.ajax({url: "libs/php/weather.php",type: "GET",

    dataType: "json",
    data: {
      north: $("#north").val(),
      south: $("#south").val(),
      east: $("#east").val(),
      west: $("#west").val(),
    },
    success: function (result) {
      console.log(result);if (result.status.name == "ok") {
        $("#outputDate").html(
          result["data"]["weatherObservations"][0]["datetime"]
        );
        $("#outputStationName").html(
          result["data"]["weatherObservations"][0]["stationName"]
        );
        $("#outputTemperature").html(
          result["data"]["weatherObservations"][0]["temperature"]
        );
        $("#outputHumidity").html(
          result["data"]["weatherObservations"][0]["humidity"]
        );
        $("#outweatherCondition").html(
          result["data"]["weatherObservations"][0]["weatherCondition"]
        );
        $("#outputWindSpeed").html(
          result["data"]["weatherObservations"][0]["windSpeed"]
        );
      }
    },
    error: function (jqXHR, textStatus, errorThrown) {
      if (data === "") {
        alert("there is an error");
      }
    },
  });
});
1

There are 1 best solutions below

0
Errorbot1122 THE SECOND On

You can just use window.onerror!

e.g.

window.onerror = function (msg, url, lineNo, columnNo, error) {
  var string = msg.toLowerCase();
  var substring = "script error";
  if (string.indexOf(substring) > -1){
    alert('Script Error: See Browser Console for Detail');
  } else {
    var message = [
      'Message: ' + msg,
      'URL: ' + url,
      'Line: ' + lineNo,
      'Column: ' + columnNo,
      'Error object: ' + JSON.stringify(error)
    ].join(' - ');

    alert(message);
  }

  return false;
};

That entire function just alerts you when you get an error!!!

Also, don't use caps on your titles