简体   繁体   English

getJson返回未定义的值,而不是Json对象

[英]getJson returns undefined value instead of Json object

I am trying to use simple getJson call to get Json object from server. 我正在尝试使用简单的getJson调用从服务器获取Json对象。

On server side everything looks good. 在服务器端,一切看起来都不错。 Json is created and returned: Json已创建并返回:

在此处输入图片说明

But on the front-end my object is not mapped to returned Json from server. 但是在前端,我的对象未映射到服务器返回的Json。 It is undefined in first call and that value and then it goes one more time through getJson and returns real object. 它在第一次调用和该值中未定义,然后再通过getJson再返回真实对象。

This is my code: 这是我的代码:

btnSaveNewMeeting.onclick = function () {
    var vm = {
        'Title': $('#eventTitle').val(),
        'StartAt': $('#startdatepicker').val() + ' ' + $('#starttimepicker').val(),
        'EndAt': $('#enddatepicker').val() + ' ' + $('#endtimepicker').val()
    }

    var meetings = GetAllScheduledMeetings();

    if (CheckIfMeetingsAreOverlapping(vm.StartAt, vm.EndAt, meetings)) {
        addNewMeetingModal.style.display = "none";

        $.ajax({
            type: 'POST',
            url: "/Meeting/Create",
            data: vm,
            success: function () {
                $('#calendar').fullCalendar('refetchEvents');
            }
        });

        ClearPopupFormValues();
    }
}

So I want to get value from GetAllScheduledMeetings(); 所以我想从GetAllScheduledMeetings();获得价值GetAllScheduledMeetings(); and put it in meetings object. 并将其放在meetings对象中

var meetings = GetAllScheduledMeetings();

function GetAllScheduledMeetings() {
    $.getJSON("/Meeting/GetAllScheduledMeetings", function (result) {
        return result;
    });
}

So GetAllScheduledMeetings() should only make a call to server and return result. 因此, GetAllScheduledMeetings()仅应调用服务器并返回结果。 But that doesn't happened. 但这没有发生。

When I debug in console of a browser what is happening is this: 1. on button click GetAllScheduledMeetings() gets invoked. 当我在浏览器的控制台中调试时,发生的事情是这样的:1.在按钮上单击GetAllScheduledMeetings()被调用。 2. getJson makes a call to server and json object is created there and returned. 2. getJson调用服务器,并在其中创建并返回json对象。 3. it goes back to front-end but skips whole return result; 3.返回前端,但跳过整个return result; part and returns undefine value to var meeting = GetAllScheduledMeetings(); 部分,并将undefine值返回给var meeting = GetAllScheduledMeetings(); 4. I get few exceptions because meeting is undefine 5. GetAllScheduledMeetings() gets called again for some reason, after all of this has happened and in this call result gets real values from server. 4.由于会议undefine我很少遇到异常GetAllScheduledMeetings() .在发生所有这些情况之后,由于某种原因再次调用GetAllScheduledMeetings() ,并且在此调用结果中从服务器获取了真实值。

Does someone knows why is this happening? 有人知道为什么会这样吗? Why two calls are made and why in the first call I do not get data from server? 为什么打了两个电话,为什么在第一个电话中我没有从服务器获取数据?

GetAllScheduledMeetings never returns anything (so, returns undefined) and "is async", so, at the moment of execution, when using meetings, there is a very high probabilty for the variable to be undefined. GetAllScheduledMeetings从不返回任何内容(因此,返回未定义)并且“是异步的”,因此在执行时,使用会议时,不确定该变量的可能性很高。 You'll need to recode GetAllScheduledMeetings to pass you all "if block" as a callback or use any other way to work with async code (Promises for instance). 您将需要重新编码GetAllScheduledMeetings,以将所有“ if block”作为回调传递给您,或使用其他任何方式来处理异步代码(例如Promises)。

function GetAllScheduledMeetings() {
    $.getJSON("/Meeting/GetAllScheduledMeetings", function (result) { // This anonymous function is called asynchronously and returns result *in the blue*
        return result;
    });
    // GetAllScheduledMeetings doesn't specify a return value so it's retVal is undefined
}

SOLUTION: 解:

Since $.getJSON or $.ajax calls are asynchronous by default all you need to do is make them synchronous by adding: async: false to call definition. 由于$.getJSON$.ajax调用默认情况下是异步的,因此您只需添加以下内容即可使它们同步: async: false来调用定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM