如何使用上一次调用的数据进行第二次 JS .post 调用
How do I make a second JS .post call with data from a previous call?
所以,我有一个 JS .post 调用,它通过控制器操作向表中添加一行。 然后,它将该行的主键返回到视图。 我需要将该整数插入到我需要对其他控制器进行的第二次 .post 调用的数据中。
更新的 Javascript首先是看一下我的Javascript:
var result = $.post('/Question/CreateSimpleQuestion/', (data), function (result) {
$.post('/Question/CreateSimpleQuestionChoice/', ({
"QuestionId":result,
"DisplayText": text,
"OrderNumber": order,
"is_correct": false}),
null, 'application/json');
}, 'application/json');
以下是调用的控制器操作:
//
// POST: /Question/CreateSimpleQuestion
[HttpPost]
public JsonResult CreateSimpleQuestion(Question question)
{
question.is_counted = true;
question.DateCreated = DateTime.Now;
db.Questions.Add(question);
db.SaveChanges();
return Json(question.QuestionId, JsonRequestBehavior.AllowGet);
}
//
// POST: /Question/CreateSimpleQuestion
[HttpPost]
public JsonResult CreateSimpleQuestionChoice(QuestionChoices choice)
{
db.QuestionChoices.Add(choice);
db.SaveChanges();
return Json(choice, JsonRequestBehavior.AllowGet);
}
为第一个 ajax 调用(密钥将可用)创建一个成功处理程序,并在该成功处理程序中进行第二个 ajax 调用,然后可以使用它传递该密钥。
var result = $.post('/Question/CreateSimpleQuestion/', data, function(result) {
// make your second ajax call here and you can use the result of the first
// ajax call here
var dataq = {
"QuestionId": result.QuestionId, // data from first ajax call here
"DisplayText": text,
"OrderNumber": order,
"is_correct": false
};
$.post('/Question/CreateSimpleQuestionChoice/', dataq, function(result2) {
// examine result of second ajax function here
// code goes here
}, 'application/json');
}, 'application/json');
这是一个嵌入了控制台调试
语句的版本,因此您可以在调试控制台中跟踪其进度,尽管我个人更喜欢只设置断点并检查变量。
console.log("Position 1");
console.log(data);
var result = $.post('/Question/CreateSimpleQuestion/', data, function(result) {
// make your second ajax call here and you can use the result of the first
// ajax call here
var dataq = {
"QuestionId": result.QuestionId, // data from first ajax call here
"DisplayText": text,
"OrderNumber": order,
"is_correct": false
};
console.log("Position 2");
console.log(dataq);
$.post('/Question/CreateSimpleQuestionChoice/', dataq, function(result2) {
// examine result of second ajax function here
// code goes here
console.log("Position 3");
console.log(result2);
}, 'application/json');
}, 'application/json');
相关文章:
- Sails.js/Waterline.add()和.remove()仅适用于第二次调用
- 提交按钮只能在onchange调用输入文本后第二次点击时工作
- 为什么第二次调用isNaN时它不起作用
- React/Redux 状态在第二次操作调用时为空
- 谷歌脚本在第二次调用Gmail后返回null
- 对已定义函数的第二次调用;不起作用
- AngularJS自定义过滤器调用了两次,并在第二次调用时删除输入数据
- Google Maps V3 fitBounds 在第二次调用具有相同边界时会缩放到更近的视图
- 电话间隙.js没有第二次调用
- 地理编码在第二次调用时返回(NaN、Nan)
- JavaScript uncatch TypeError:无法在第二次迭代时调用未定义的方法“拼接”
- JavaScript函数不可见?不会第二次被调用
- 让Facebook好友first_name无需第二次API调用
- 如何使用上一次调用的数据进行第二次 JS .post 调用
- 我的 js 函数在第二次单击鼠标后被调用
- 第二次调用主干时在回调中丢失上下文
- 第一次点击失败,第二次点击在 Ajax 调用 • JQuery / PHP 上成功
- jQuery UI 对话框在第二次加载时不会调用函数
- Ajax调用第二次不工作
- ajax调用第二次不起作用