0

我最近重构了我的 javascript/jquery 应用程序代码以使用模块模式。

我有两个模块,比如说 A 和 B。

模块 B 包含一个公共方法(例如,Bmethod),它进行 jQuery AJAX JSONP 调用($.ajax)并在回调中传递响应。

在模块 A 中有一个对 B.Bmethod() 的调用,其中包含一个回调函数来处理返回的响应。

这是模块B的定义:

var B = (function()
{
  var obj = {};
  obj.Bmethod = function(data, callback)
                {
                  //do JSONP AJAX call
                  callback(response);
                }
  return obj;
}());

现在,这是模块 A 的定义以及模块 B 上的方法调用

var A = (function()
{
  var doAjax = function(data)
  {
    B.Bmethod(data, function(response)
    {
       //Do something with the response
    });
  }    
}());

这是我加载模块 A 并开始执行代码的方式:

$(document).ready(function()
{
   A.doAjax(data);   
});

这里的问题是,我在 Chrome 和 Firefox 上有不同的行为。在 Chrome 上,根本没有执行 AJAX 调用。没有发送请求。但是,在 Firefox 上,我可以看到发出的请求并得到响应,但我没有收到成功回调。

如果我将所有这些代码放在模块之外,只放在一个文件中,一切似乎都可以正常工作。

我已经看到很多人(也在 StackOverflow 上)成功使用模块模式和 AJAX 调用,但无法弄清楚我做错了什么。

有什么想法/解决方案吗?

4

1 回答 1

0

Fixed the problem.

I was actually having a duplicate copy of the same AJAX request($.ajax) in a different file. So, when the request was being made, I am assuming the duplicate was (also)being called, and maybe this was causing inconsistency issues. However, on Chrome, setting a breakpoint showed no signs of the duplicate being called.

For now, I removed the duplicate request and it works fine. But, I still don't understand why there was an inconsistent behavior between Chrome and Firefox.

于 2012-07-09T20:40:30.103 回答