如果语句基于 return if 一个 $.post 命令
If statement based on return if a $.post command
我有以下代码:
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);
相关文章:
- Javascript XMLHttpRequest——只有第一个POST请求有效
- 如果语句基于 return if 一个 $.post 命令
- Jquery:如何不允许下一个 $.post(),直到解决前一个
- jQuery File Upload插件如何将文件上传到服务器——每个文件都在一个POST请求中,或者全部在一个请求中
- Mocha正在测试一个post函数
- 一键提交GET表单和另一个POST表单
- 在表单操作之前先发一个$.post
- JQuery:在链接被关注之前执行一个post
- ExpressNodejs-如何将request.files传递到另一个POST请求
- Ajax发送一个get-to-original页面,而不是一个post-to-different页面
- 为什么我不能通过AngularJS's post发送一个post到我的WebApi ?
- Ajax:第一个Post很棒,第二个Post返回URL中的数据
- 添加一个POST参数到fb:login按钮
- 显示一个下拉列表的选定值在一个文本字段(javascript)在一个post wordpress
- 在jQuery中获取URL并创建一个post link ajax
- 三个文本输入到一个POST名称
- 提交一个post请求与json数据和rest服务将生成excel文件,并通过流返回
- 如何让骨干模型做一个POST对fetch
- 如何使一个POST与ajax就像表单提交
- 下载一个文件发送响应一个POST请求通过nodejs?(没有AJAX)