碰到一bug,调试一下记录一下过程:

问题: js中通过jqeuery 的Deferred object 发送http 请求,其请求正确,但是每次请求没有返回,页面直接跳转。

具体的:在写js文件中,有这么一段代码,发送一个http 请求:

queryInconsistentTables= function(){

  var dtd = $.Deferred();
        ajax({
                        url: this.__sBaseUrl + 'query'
                        type: 'GET',
                        contentType: 'application/json',
                        dataType: 'json',
                    }).done(function(response) {                    
                    dtd.resolve(response);
                }).fail(this.serverErrorHandler(dtd));
                
                return dtd.promise();
}

在代码中这样调用:

 $.when(f())                        
 .done(function(response){
   // todo
}) .fail(function(response){ // error handling }) .always(function(){ });

但是每次运行,页面都会刷新。奇怪,http调用其返回压根没有进入done/fail/always 的任一个分支。

严重怀疑http本身返回值有问题,通过XHR Post 查看,发现其返回值为

{v1:[],v2:[]}

查看发现json的返回值没有引号,是不合法的json。 问题找到了,加上引号,问题解决了。 一切运行如期望。

 

2. 问题是为什么返回值的格式出错,其页面就会刷新?
 

  初步猜测: http请求返回值有问题,但是毕竟返回。有问题只可能是Deferred object 封装的转换出问题: 转换没有成功,却不知为何刷新页面。

  小心求证:

   替换掉jquery-1.7.2.min.js ,为jquery-1.7.2.js 在chrome 中调试一下。 

   这是代码   http://code.jquery.com/jquery-1.7.2.js  

   下面分析一下这个代码jQuery.ajax 

    http://james.padolsey.com/jquery/#v=1.7.2&fn=jQuery.ajax

  

 

http://json.org

http://code.jquery.com/jquery-1.7.2.js

http://james.padolsey.com/jquery/#v=1.7.2&fn=jQuery.ajax (good way to ready jquery code)

 

 

 

posted on 2013-05-31 17:12  RocZhang  阅读(185)  评论(0编辑  收藏  举报