如果声明基于返回,如果是$ .post命令

时间:2013-09-17 19:16:45

标签: javascript jquery post if-statement

我有以下代码:

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

2 个答案:

答案 0 :(得分: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
    }

}

答案 1 :(得分:0)

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);