0

Yet another issue with my code. I'm trying to attach a function in the event of a successful request, but it doesn't seem to be working. My code so far:

const nbg = function() {
  $.ajax({
    url: "http://rmpc/json/notices.json",
    method: "GET",
    dataType: "json",
    cache: false,
    success: function (data) {
      alert("WORKS");
    }
  });
};

Yet, whenever I try success: alert("WORKS") it suddenly works. I'm incredibly confused by this. Any ideas? Thanks

Edit: 'rmpc' is a local web server on my home network, might I add

RWN
  • 99
  • 2
  • 9
  • Is the response from the file valid json? An ajax request that has `dataType: 'json'` on it **must** return valid json, even if it is just `{}`. jQuery will try to parse the response in this case and if it is not valid, or not set, it will error, regardless of if the response was a 200 or not. – Taplar Mar 03 '18 at 00:12
  • I hope it helps [Ajax success function](https://stackoverflow.com/questions/22892706/ajax-success-function) –  Mar 03 '18 at 00:12
  • 1
    Check the browsers console. You have be facing a CORS problem. – Pedro Martins Mar 03 '18 at 00:14
  • `success: alert("WORKS")` will fire when page load since `()` will trigger it. try to paste `$.ajax({ url: "http://rmpc/json/notices.json", method: "GET", dataType: "json", cache: false, success: alert("WORKS") });` in console – Theo Mar 03 '18 at 00:15
  • As far as I can tell. This is the file: { "notices":[ { "name":"Raphael", "notice":"teststetset", "dati":"02/03/2018 23:27" }, ]} – RWN Mar 03 '18 at 00:15
  • 1
    I see a trailing comma. `"dati":"02/03/2018 23:27" },` remove the comma – Taplar Mar 03 '18 at 00:16
  • @Taplar That worked, thanks – RWN Mar 03 '18 at 00:19
  • K, since it was just a comma in a file, i'm going to flag as typo for close. – Taplar Mar 03 '18 at 00:19
  • 1
    Well `success: alert("WORKS")` is calling alert and assigning what it returns to success. SInce alert does not return anything, it is basically doing `success: undefined` – epascarello Mar 03 '18 at 00:21
  • If you're getting a trailing comma in the JSON, it must mean that you're trying to create the JSON by hand, instead of using a library. **Don't do that**. You will invariably do it wrong. – Barmar Mar 03 '18 at 00:22

1 Answers1

3

It means that request is not success, so it must be an error. You need to catch it as well to find out:

const nbg = function () {
  $.ajax({
    url: "http://rmpc/json/notices.json",
    method: "GET",
    dataType: "json",
    cache: false,
    success: function (data) {
      alert("WORKS");
    },
    error: function (xhr, error) {
      console.debug(xhr); 
      console.debug(error);
    }
  });
};
jdaz
  • 5,964
  • 2
  • 22
  • 34
Theo
  • 1,932
  • 4
  • 17
  • 40
  • I think you missed that I did try `success: alert("Works")`, which did work, so the request is a success. I know it has to be a syntax problem. (or something else) – RWN Mar 03 '18 at 00:17
  • `success: alert("WORKS")` will fire when page load since () will trigger it. try to paste `$.ajax({ url: "http://rmpc/json/notices.json", method: "GET", dataType: "json", cache: false, success: alert("WORKS") })` in chrome console - it will alert immediately; – Theo Mar 03 '18 at 00:17
  • could be the `trailing comma` in the JSON file that you send as well. see comment on your question above :) – Theo Mar 03 '18 at 00:19