jQuery AJAX中的成功函数无法正常工作[关闭]

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

我的代码的另一个问题。我正在尝试在成功请求的情况下附加函数,但它似乎不起作用。我的代码到目前为止:

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

然而,每当我尝试success: alert("WORKS")时,它突然起作用。我非常困惑。有任何想法吗?谢谢

编辑:'rmpc'是我家网络上的本地Web服务器,我可以添加

javascript jquery ajax
1个回答
1
投票

这意味着请求不成功所以必须是一个错误。你需要抓住它以找出答案

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);
    },
  });
};
© www.soinside.com 2019 - 2024. All rights reserved.