0

我有以下代码:

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 输入时,我收到的错误返回为Section/undefined但点 1 和 2 的警报字符串具有正确的值,所以我无法理解为什么这些在设置中未定义或 urlstring 我确定我在做一些非常愚蠢的事情,但我似乎看不出它是什么任何帮助将不胜感激

4

2 回答 2

1

使用回调函数的可能解决方案,因为 $.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
    }

}
于 2013-09-17T20:10:40.390 回答
0

var idonce 的定义放在第一个之外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);
于 2013-09-17T19:29:40.910 回答