简体   繁体   English

JavaScript返回无效吗? 返回未定义

[英]Javascript return doesn't work? returns undefined

I got a wierd issue with a javascript script. 我遇到了一个JavaScript脚本的怪异问题。

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

function checkDiscountCode()
{

  var discountCode = $('#korcode').val();
  var errorMessage = $('#errorMessage').text();

  if(discountCode.length > 2)
  {

     $.getJSON( 'calc.php?code=' + discountCode, function( json ) 
     {    
       var color = json.color; 
       var soort = json.soort;
       var waarde = json.waarde; 
       var message = json.message;

       if(errorMessage != message)
       {
          $('#error').html('<font color="' + color + '" id="errorMessage">' + message + '</font>');
       }

       if(message == 'OK!')
       {
          var outputData = soort + '-' + waarde;
          console.log('outputData = ' + outputData);
          return outputData;
       }

     });   
  }   
}

The console log give the right data, for example 0-12.00 but when I request this function in another function, for example just a alert(checkDiscountCode()); 控制台日志会提供正确的数据,例如0-12.00,但是当我在另一个函数中请求此函数时,例如,只是一个alert(checkDiscountCode()); it says undefined.. 它说未定义..

Can anyone please help me? 谁能帮帮我吗? I don't see the issue.. 我没有看到这个问题。

Thanks in advance! 提前致谢!

Your checkDiscountCode() function doesn't have a return statement, so it returns undefined by default. 您的checkDiscountCode()函数没有return语句,因此默认情况下返回undefined

The return statement that you've included is inside the anonymous function passed as a callback for $.getJSON() , so it returns from that function back to the jQuery code that called it, not back to your code (and even then it returns undefined if message is not 'OK!' ). 您所包含的return语句位于$.getJSON()的回调传递的匿名函数内部,因此它从函数返回到调用它的jQuery代码,而不是返回到您的代码(甚至返回如果message不是'OK!'则为undefined)。 (Also your $.getJSON() doesn't run unless discountCode.length > 2 , though your checkDiscountCode() function doesn't try to return a value either way.) (此外,除非discountCode.length > 2 ,否则$.getJSON()不会运行,尽管您的checkDiscountCode()函数不会尝试以两种方式返回值。)

If discountCode.length > 2 is not true, you aren't returning a value, thus it will return undefined. 如果discountCode.length > 2不为true,则不会返回任何值,因此它将返回undefined。


I'll also add that if you did put it in another function alert(checkDiscountCode()) and you closed firebug up, or opened in another browser that doesn't have console open, your browser will encounter a javascript error, if you left the console.log(...) line still in your code. 我还要补充一点,如果您确实将其放在另一个函数alert(checkDiscountCode())并且关闭了Firebug,或者在没有打开控制台的其他浏览器中打开,则如果您离开了浏览器,则会遇到JavaScript错误console.log(...)行仍在您的代码中。

A console window has to be open in order to log to it. 必须打开控制台窗口才能登录。

You're returning from a callback passed to a function that does work asynchronously . 您正在从传递给确实异步工作的函数的回调中返回。 You'll have to adapt the code to take a callback and change the caller to deal with the fact that it's asynchronous. 您必须调整代码以进行回调,并更改调用方以处理它是异步的事实。

That means that rather than this: 那意味着不是这个:

// won't work
function checkSomething() {
    someAsynchronousFunction(function(result) {
        return result == "success";
    });
}
if(checkSomething()) {
    alert("It worked.");
}else{
    alert("It didn't work.");
}

You'll have to change your code to look like this: 您必须将代码更改为如下所示:

function checkSomething(callback) {
    someAsynchronousFunction(function(result) {
        callback(result == "success");
    });
}
checkSomething(function(result) {
    if(result) {
        alert("It worked.");
    }else{
        alert("It didn't work.");
    }
});

JQuery's getJSON() method runs asynchronously. jQuery的getJSON()方法异步运行。 You should add a callback function to checkDiscountCode(); 您应该在checkDiscountCode()中添加一个回调函数; the callback function will run the rest of your app's logic. 回调函数将运行应用程序的其余逻辑。 For example: 例如:

function sampleCallback(outputData) {
  console.log('outputData = ' + outputData);
}

function checkDiscountCode(callback) {
  $.getJSON( 'calc.php?code=' + discountCode, function( json ) {
    // get output data, and then call callback.
    callback(outputData);
  });
}

checkDiscountCode(sampleCallback);

And if discountCode.length > 2 you are invoking the $.getJSON function, but that takes a callback, and returns nothing immediately. 如果discountCode.length > 2您正在调用$.getJSON函数,但这需要回调,并且立即不返回任何内容。 Hence, in either case, you get undefined . 因此,无论哪种情况,您都不undefined

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

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