2013-05-18 91 views
0

背景:我從ajax插入到外部php文件的頁面內容。
約束條件:我想遵循How to 「properly」 create a custom object in JavaScript?中描述的對象結構。
問題:但是,隨着這種結構,我沒有收到成功回調響應數據。在firebug中,我可以看到正確的 html,它是通過郵件返回的。回調成功功能沒有收到數據

問題的代碼:

// Abstract class for functionality of both QuizView and QuizAdmin 
Quiz = Object.makeSubclass(); 
Quiz.prototype._init= function() {}; 
Quiz.prototype.fetchSuccess= function(data) { 
    // fetchSuccess must be defined before fetch. 
    console.log("Quiz.prototype.fetchSucces"); // This gets printed out - so the function is called. 
    console.log(this.targetElement); // echoes the correct element - when I hoover it, I see it in the page. 
    console.log(data); // echoes undefined 
    this.targetElement.append(data); // Obviously this line is what I want to do. 
}; 
//This function gets content specified by url. 
Quiz.prototype.fetch= function(what,where) { 
    console.log("Quiz.prototype.fetch"); 
    this.targetElement=where; 
    console.log(this.targetElement); 
    // Get the the content 
    $.ajax({ 
      type: "POST", 
      url: what, 
      success: this.fetchSuccess, 
      targetElement: this.targetElement, 
      dataType: "html", 
      async: false 
    }); 
}; // End Quiz.prototype.fetch 

注:相關問題之間,我的代碼基本上是唯一的區別波紋管,我使用function expression,而不是function declaration。這是因爲我想遵循限制中描述的結構。

相關問題:

Jquery ajax external callback function
JavaScript: var functionName = function() {} vs function functionName() {}
Where to define a jQuery $.ajax() success function if you don't want to define in the call to $.ajax()?
jQuery ajax success callback function definition

簡單的代碼組織(這個作品!)

function fetchSuccess(data){ 
    $(".content-bottom").append(data); 
} 

    $(document).ready(function(){ 
    // Get the view content 
    $.ajax({ 
     type: "POST", 
     url: "../../../../includes/quiz1/index.php", 
     success: fetchSuccess, 
     dataType: "html", 
     async: false 
    }); 
    }); 

回答

1

試試這個

你傳入函數引用和thisfetchSuccess是不是你期待什麼

// Abstract class for functionality of both QuizView and QuizAdmin 
Quiz = Object.makeSubclass(); 
Quiz.prototype._init= function() {}; 
Quiz.prototype.fetchSuccess= function(data) { 
    // fetchSuccess must be defined before fetch. 
    console.log("Quiz.prototype.fetchSucces"); // This gets printed out - so the function is called. 
    console.log(this.targetElement); // echoes the correct element - when I hoover it, I see it in the page. 
    console.log(data); // echoes undefined 
    this.targetElement.append(data); // Obviously this line is what I want to do. 
}; 
//This function gets content specified by url. 
Quiz.prototype.fetch= function(what,where) { 
    console.log("Quiz.prototype.fetch"); 
    this.targetElement=where; 
    console.log(this.targetElement); 
    // Get the the content 
    var self = this; 
    $.ajax({ 
      type: "POST", 
      url: what, 
      success: function(){self.fetchSuccess.apply(self, arguments)}, 
      targetElement: this.targetElement, 
      dataType: "html", 
      async: false 
    }); 
}; 
+2

是不是'成功:函數(){self.fetchSuccess.apply(個體經營,參數)}'等於'成功:自.fetchSuccess'? – JAM

+0

@JAM不​​,完全沒有。成功:self.fetchSuccess將設置與其原始範圍不同的fetchSuccess範圍。 – Anoop

+0

我不同意,看看這個小提琴:http://jsfiddle.net/rPFFR/ – JAM