【问题标题】:Issue related to getting value from AJAX Call in Famo.us与从 Famo.us 中的 AJAX 调用获取价值相关的问题
【发布时间】:2014-06-16 19:49:36
【问题描述】:

我是 Famo.us 框架的新手。我在 Famo.us 框架中有一个小应用程序。

我有以下代码:

define(function (require, exports, module) {
    var Surface = require('famous/core/Surface');
    var Modifier = require('famous/core/Modifier');
    var Transform = require('famous/core/Transform');
    var View = require('famous/core/View');
    var HeaderFooterLayout = require('famous/views/HeaderFooterLayout');
    var App = require('./App');


    var apps = [];

    GetContent.prototype = Object.create(View.prototype);
    GetContent.prototype.constructor = GetContent;
    GetContent.DEFAULT_OPTIONS = {};

    function GetContent() {
        View.apply(this, arguments);
        GetData();
    }

    function GetData() {
        $.ajax({
            type: "GET",
            url: "/LocalPlatFormService.svc/GetJobRCompanies",
            contentType: "application/json; charset=utf-8",
            success: ajaxCallSucceed,
            dataType: "json"
        });
    }

    function ajaxCallSucceed(response) {
        var finalStr = '';
        var a = response[0].Success;
        if (a.toString().toLowerCase() == "true") {
            for (var i = 0; i < response.length; i++) {
                var strId = response[i].Id;
                var strSuccess = response[i].Success;
                finalStr += i + '.' + strId + ' , ' + strSuccess + ' ';

                var app1 = new App({});
                app1.AddPages(response[i].ImageLarge, response[i].Description, response.length, i);
                apps.push(app1);
            }
        }
        else {
            alert("No Data Found.");
        }
    }

    module.exports = GetContent;

});

GetData() 中的 Ajax 调用工作正常,并且它还在成功调用时调用 ajaxCallSucceed()。但问题是ajaxCallSucceed() 在执行所有其他函数之后执行,即使在最后一行执行module.exports = GetContent; 之后也是如此。

我想根据从数据库中获取数据来绑定ajaxCallSucceed() 中的一些值 但是如何从ajaxCallSucceed() 获取值,因为它是在所有其他操作之后执行的。

谢谢。

【问题讨论】:

    标签: javascript jquery famo.us


    【解决方案1】:

    将 ajax 异步设置为 false。像这样:

    $.ajax({
    type: "GET",
    async: false,
    url: "/LocalPlatFormService.svc/GetJobRCompanies",
    contentType: "application/json; charset=utf-8",
    success: ajaxCallSucceed,
    dataType: "json"
    

    });

    将 async 设置为 false 意味着您正在调用的语句必须在函数中的下一条语句被调用之前完成。如果您设置 async: true 则该语句将开始执行,并且无论异步语句是否已完成都将调用下一条语句。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-06
      • 2018-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多