YUI3 - 为什么事件处理程序没有在GET请求上触发?

时间:2012-09-04 23:46:23

标签: yui yui3

我正在使用YUI3来执行GET请求,但是当我尝试执行GET请求时,事件处理程序没有触发。注释应该记录到每个事件处理程序中的控制台。这是我的代码:

YUI({ filter: 'raw' }).use("io-xdr", "substitute", "json-parse", "node", function(Y) {

  var url = "http://localhost:8000/scripts/test.php";

  var output = Y.one("#container");  

  var cfg = {
    method: "GET",
    xdr: {
      use: 'native'
    },  
    on: {
      start: handleStart,
      success: handleSuccess,
      failure: handleFailure,
    }
  };

  var handleStart = function(id, a) {
    output.set("innerHTML", "YES");
    console.log("Inside of handleStart");
    Y.log("a");
  };

  var handleSuccess = function(id, o, a) {
    var results = Y.JSON.parse(o.responseText);
    console.log(results.count);
    console.log(results);
    Y.log("b");
  };

  var handleFailure = function(id, o, a) {
    console.log("Inside of handleFailure");
    Y.log("c");
  };

  var obj = Y.io(
    url, cfg
  );

});

控制台中没有任何错误。网址是正确的。

1 个答案:

答案 0 :(得分:2)

在您定义cfg的位置上方声明您的处理程序。 JavaScript使用提升,所以变量在技术上是“可用的”,虽然在您尝试使用它们时未定义。

您可以看到此jsbin显示的功能有效:http://jsbin.com/owemib/1/edit

虽然使用CORS它不起作用,但它至少会记录!