如果语句基于 return if 一个 $.post 命令

If statement based on return if a $.post command

本文关键字:一个 post 命令 if return 语句 如果      更新时间:2023-09-26

我有以下代码:

if (localStorage["Course" + '@Model.Course.CourseID'] != null && localStorage["Course" + '@Model.Course.CourseID'] != "")
{
  var courseSectionID = localStorage["Course" + '@Model.Course.CourseID'];
  alert("BEFORE :" + courseSectionID);
  var postData = { 'courseSectionID': courseSectionID };
  $.post('/Course/CourseSectionLaunchStillExistCheck/', postData, function (data)
  {
    if (data == "True") 
    {
      var id = parseInt(courseSectionID);
      alert("1: " + id);
    }
    else
    {
      var id = '@Model.CourseSections.First().CourseSectionID';
      alert("2: " + id);
    }
  });
}
else
{
  var id = '@Model.CourseSections.First().CourseSectionID';
  alert("3: " + id);
}
var urlstring = 'Section/' + id;
alert(urlstring);

如果我在警报 3 处输入,我总是按预期获得正确的 Section/115 等,但是当我通过警报点 1 和 2 输入时,我收到错误返回为部分/未定义,但点 1 和 2 的警报字符串具有正确的值,所以我无法理解为什么这些在设置或 urlstring 中未定义,我确定我在做一些非常愚蠢的事情,但我似乎不能看看它是什么任何帮助将不胜感激

使用回调函数的可能解决方案,因为 $.post() 发出的 ajax 请求默认情况下是异步的。我还没有测试代码。

//receives a callback function as parameter to print the id.
myFunction(printFunction);//see declaration of printFunction below

//prints the id
function printFunction(id) {
    var urlstring = 'Section/' + id;
    alert(urlstring);
}

function myFunction(callbackFunctionAsParameter) {
    if (localStorage["Course" + '@Model.Course.CourseID'] !== null && localStorage["Course" + '@Model.Course.CourseID'] !== "") {
        var courseSectionID = localStorage["Course" + '@Model.Course.CourseID'];
        var postData = {
            'courseSectionID': courseSectionID
        };
        //by default all ajax requests are sent asynchronously in jquery => http://api.jquery.com/jQuery.ajax/
        $.post('/Course/CourseSectionLaunchStillExistCheck/', postData, function (data) {
            var id = null;
            if (data == "True") {
                id = parseInt(courseSectionID);
            } else {
                id = '@Model.CourseSections.First().CourseSectionID';
            }
            callbackFunctionAsParameter(id); //print here
        });
    } else {
        var id = '@Model.CourseSections.First().CourseSectionID';
        callbackFunctionAsParameter(id); //print here
    }
}

var id的定义放在第一个if之外,如下所示并尝试。

var id = null;
if (localStorage["Course" + '@Model.Course.CourseID'] != null && localStorage["Course" + '@Model.Course.CourseID'] != "")
{
  var courseSectionID = localStorage["Course" + '@Model.Course.CourseID'];
  alert("BEFORE :" + courseSectionID);
  var postData = { 'courseSectionID': courseSectionID };
  $.post('/Course/CourseSectionLaunchStillExistCheck/', postData, function (data)
  {
    if (data == "True") 
    {
      id = parseInt(courseSectionID);
      alert("1: " + id);
    }
    else
    {
      id = '@Model.CourseSections.First().CourseSectionID';
      alert("2: " + id);
    }
  });
}
else
{
  id = '@Model.CourseSections.First().CourseSectionID';
  alert("3: " + id);
}
var urlstring = 'Section/' + id;
alert(urlstring);