Javascript Function With JQuery POST Always Returns undefined

后端 未结 5 1658
情书的邮戳
情书的邮戳 2020-12-20 02:35

I have no idea what\'s going on here and was hoping someone could help, I\'m sure it\'s something easy going on that I\'m just missing.

I have a function in javascri

5条回答
  •  Happy的楠姐
    2020-12-20 03:09

    You can't do it like that. Remember, the "A" in AJAX means "Asynchronous". The callback function you provide to $.post() will execute well after GetTotalSize() executes and returns.

    You'll need to restructure your code to accommodate this. I can't be specific in my recommendation because I don't know what the rest of your code looks like, but here's a possibility.

    $.post("Handlers/GetTotal.ashx", {id : $("#hID").val()}, function(data)
    {
      doSomethingWithTotalSize( data.toString() );
    });
    
    function doSomethingWithTotalSize( totalSize )
    {
      // whatever
    }
    

提交回复
热议问题