Почему вызов AJAX игнорируется? - PullRequest
0 голосов
/ 22 мая 2019

Я не знаю, почему процесс игнорирует мой вызов 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");

}

1 Ответ

1 голос
/ 22 мая 2019

Ваш код не вызывает функцию ajax jQuery, он переназначает ее.

$.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");
    }
  });
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...