javascript - 为什么 AJAX 调用会被忽略?

标签 javascript jquery ajax

我不知道为什么该进程会忽略我的 AJAX 调用。它只是从 console.log("1"); 跳转到 console.log("2");

有人可以向我解释一下出了什么问题吗?

render: function() {
  let view = this;
  component.prototype.render.call(view);

  console.log("1");

  $.ajax = ({
    type: "GET",
    cache: false,
    url: "news.json",
    dataType: "json",
    success: function(json) {

      console.log("success");

      for (let i = 0; i < json.length; i++) {
        let news = modelNews;
        news.title = json[i].title;
        news.type = json[i].type;
        news.img = json[i].img;
        news.link = json[i].link;

        view.$('#newsfeed').append(news.getNewsFeedLook());
      }
    },
    error: function() {
      console.log("error");
    }
  });

  console.log("2");

}

最佳答案

您的代码没有调用 jQuery 的 ajax 函数,而是重新分配它。

$.ajax = ({
    type: "GET",
    cache: false,
    url: "news.json",
    dataType: "json",
    success: function(json) {

      console.log("success");

      for (let i = 0; i < json.length; i++) {
        let news = modelNews;
        news.title = json[i].title;
        news.type = json[i].type;
        news.img = json[i].img;
        news.link = json[i].link;

        view.$('#newsfeed').append(news.getNewsFeedLook());
      }
    },
    error: function() {
      console.log("error");
    }
  });

这是正确的调用,即函数调用。 请密切注意像这样的小错误!

$.ajax({
    type: "GET",
    cache: false,
    url: "news.json",
    dataType: "json",
    success: function(json) {

      console.log("success");

      for (let i = 0; i < json.length; i++) {
        let news = modelNews;
        news.title = json[i].title;
        news.type = json[i].type;
        news.img = json[i].img;
        news.link = json[i].link;

        view.$('#newsfeed').append(news.getNewsFeedLook());
      }
    },
    error: function() {
      console.log("error");
    }
  });

关于javascript - 为什么 AJAX 调用会被忽略?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56258457/

相关文章:

javascript - 所见即所得的 CSS 编辑器

javascript - 如何使用jspdf自动分页

javascript - 用逗号分割字符串并格式化后显示

javascript - Angular2 路由与 Express 路由结合使用?

javascript - child 的 z-index 被 parent 覆盖

javascript - json值的长度?

javascript - JQuery AJAX 同步请求行为

php - 图表仅使用 jquery ajax php 打印出第一个值

javascript - 如何使用PHP ajax在Modal Box中提交HTML表单数据?

javascript - 迷失了制作秒表的尝试