如何通过模块化js结构中的ajax调用接收数据? [重复]

时间:2022-03-23 15:02:25

This question already has an answer here:

这个问题在这里已有答案:

Its a very basic java-script with modular structure, basically what I'm trying to do is , requesting a random quote via an API, printing them on the HTML page via Mustache.js. Earlier without using the modular structure way, I managed to accomplish this task, but I wanted to try the modular way too.

它是一个非常基本的java脚本,具有模块化结构,基本上我正在尝试做的是,通过API请求随机引用,通过Mustache.js在HTML页面上打印它们。之前没有使用模块化结构方式,我设法完成了这项任务,但我也想尝试模块化方式。

The problem i'm facing now is that whenever i try to render my data (i.e quote + author) , I recieve an error on my console that the function is not defined.

我现在面临的问题是,每当我尝试渲染我的数据(即引用+作者)时,我在控制台上收到一个错误,即函数未定义。

Please check my code ~

请检查我的代码〜

        (function (){
      var quoting ={
        quotei : [],
        template : $("#quoteTemplate").html(),
        init: function (){
          this.cacheDom();
           this.bindEvents();
          this.createQuote();
          this.recieve();
          this.renderx();

        },

        cacheDom: function(){
          this.$el = $('#quotez');
          this.$button = this.$el.find('button');
          this.$template = this.$el.find('#quoteTemplate').html();

        },

        bindEvents: function(){
          this.$button.on('click',this.createQuote.bind(this));

        },

        renderx: function(data){

            this.$el.html(Mustache.render(this.template,data));

          },

        createQuote: function(){

        $.ajax({
           url:'https://andruxnet-random-famous-quotes.p.mashape.com/?cat=famous',
           type:'GET',
           data:{},
           dataType:'json',
           success : function(data){;
               this.render(data)

            },
           beforeSend: function(xhr){
             xhr.setRequestHeader("X-Mashape-Authorization","cvkQkHJurZmshuIhXxwXzIjBchHVp1yk0rDjsnNambAJ9duu7v");
             }
            });

          }, 

      };
      quoting.init();

      })()

Please help me out and pardon for any mistakes , as this is my first time posting on *.

请帮助我解决任何错误,因为这是我第一次在*上发布。

1 个解决方案

#1


2  

Here is the refactored code

这是重构的代码

Working Demo: here Output: [object Object] { author: "Martin Luther King Jr.", category: "Famous", quote: "In the End, we will remember not the words of our enemies, but the silence of our friends." }

工作演示:这里输出:[object Object] {作者:“Martin Luther King Jr.”,类别:“着名”,引用:“最后,我们不会记住敌人的话语,而是我们朋友的沉默“。 }

Code:

(function ($) {

  function quoting() {

    this.quotei = [];
    this.template = $("#quoteTemplate").html();
    this.init();
  }

  quoting.prototype = {
    init: function () {
      this.cacheDom();
      this.bindEvents();
      this.createQuote();
      //this.recieve(); 
      //this.renderx(); 
    },

    cacheDom: function(){
      this.$el = $('#quotez');
      this.$button = this.$el.find('button');
      this.$template = this.$el.find('#quoteTemplate').html();
    },

    bindEvents: function(){
      this.$button.on('click', this.createQuote.bind(this));
    }, 

    renderx: function(data) {
        console.log(data);
        //this.$el.html(Mustache.render(this.template,data));
    },

    createQuote: function(){
    var self = this;
    $.ajax({
       url:'https://andruxnet-random-famous-quotes.p.mashape.com/?cat=famous',
       type: 'GET',
       dataType: 'json',
       beforeSend: function(xhr) {
         xhr.setRequestHeader("X-Mashape-Authorization","cvkQkHJurZmshuIhXxwXzIjBchHVp1yk0rDjsnNambAJ9duu7v");
       }
     }).done(function(data) {
          self.renderx(data);
    })

   } 

  };

var myQuote = new quoting();

})(window.jQuery);

#1


2  

Here is the refactored code

这是重构的代码

Working Demo: here Output: [object Object] { author: "Martin Luther King Jr.", category: "Famous", quote: "In the End, we will remember not the words of our enemies, but the silence of our friends." }

工作演示:这里输出:[object Object] {作者:“Martin Luther King Jr.”,类别:“着名”,引用:“最后,我们不会记住敌人的话语,而是我们朋友的沉默“。 }

Code:

(function ($) {

  function quoting() {

    this.quotei = [];
    this.template = $("#quoteTemplate").html();
    this.init();
  }

  quoting.prototype = {
    init: function () {
      this.cacheDom();
      this.bindEvents();
      this.createQuote();
      //this.recieve(); 
      //this.renderx(); 
    },

    cacheDom: function(){
      this.$el = $('#quotez');
      this.$button = this.$el.find('button');
      this.$template = this.$el.find('#quoteTemplate').html();
    },

    bindEvents: function(){
      this.$button.on('click', this.createQuote.bind(this));
    }, 

    renderx: function(data) {
        console.log(data);
        //this.$el.html(Mustache.render(this.template,data));
    },

    createQuote: function(){
    var self = this;
    $.ajax({
       url:'https://andruxnet-random-famous-quotes.p.mashape.com/?cat=famous',
       type: 'GET',
       dataType: 'json',
       beforeSend: function(xhr) {
         xhr.setRequestHeader("X-Mashape-Authorization","cvkQkHJurZmshuIhXxwXzIjBchHVp1yk0rDjsnNambAJ9duu7v");
       }
     }).done(function(data) {
          self.renderx(data);
    })

   } 

  };

var myQuote = new quoting();

})(window.jQuery);