简体   繁体   English

我已经在jQuery中收到函数未定义错误,即使该函数已定义

[英]I am getting function undefined error in jQuery, even though the function is defined

This is what I have in my code to get the session info from the controller via ajax. 这就是我的代码中要通过ajax从控制器获取会话信息的内容。

function getMySession() {
    $.ajax({
        url: 'ajax-get-actual-session',
        type: 'POST',
        dataType: 'json',
        success: function(response) {
            return response;
        }
    });
}

getMySession()
.done(function(r) {
    if (r) {
        alert(JSON.stringify(r));
    } else {
        alert('bad');
    }
})
.fail(function(x) {
    alert('even worse');
});

But I am getting in Firefox console an error: 但是我在Firefox控制台中遇到一个错误:

TypeError: getMySession(...) is undefined

However, I have already defined it in my code. 但是,我已经在代码中定义了它。 Why is that? 这是为什么? What am I doing wrong? 我究竟做错了什么?

Any idea, how can i make this code work? 任何想法,我如何使此代码工作?

You need to return the promise 你需要兑现承诺

function getMySession() {
  return $.ajax({ ...

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

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