weixin_33725722 2015-02-26 18:44 采纳率: 0%
浏览 345

Javascript跳过Ajax请求

There is an ajax request which retrieves array of data from spring service.

This is the exact code snippet:

getData: function() {
            var me = this;
            var response = [];
            Backbone.ajax({
                url: 'http://localhost:8080/filters/clients',
                success: function(result) {
                    this.response = me.getFilterValues(result);
                    console.log(this.response+" response");
                }
            });
            return this.response;
        },

This is the call to this function from other model

this.set('data', DataProvider.getData(/*pass all selected values here*/));

As you can see on successful ajax request it returns data and passes it to other function which returns an array and then is being used to render view. After some debugging I noticed that at first when this set statement is called ajax is being skipped and so it returns value of undefined. After some time when it tries to access this undefined data object it finally does this ajax successfully but at that point its too late.

Why? Is it because its async and first time it gets the request is being sent and without any wait it goes straight to the next portion of code or what? Debugging shows that first time it comes to ajax it just gets the name and return line thats it.

  • 写回答

1条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥30 如何降低hdfs中datanode的JVM内存用量
      • ¥15 Android URL如何转成视频/音频,可行吗?
      • ¥20 SQL数据查询,子查询
      • ¥15 c++字符串分割问题
      • ¥15 vue+uniapp
      • ¥15 android freedom
      • ¥15 使用自定义的类型代替内置类型可行吗
      • ¥15 关于STM32的SPI和ENDAT接口编码器通信的问题
      • ¥15 关于#pdfbox#生成的PDF文件正常,转图片中文乱码的问题,如何解决?
      • ¥15 ADS中有关DAC控件的使用问题