gpt4 book ai didi

c# - 如何使用上一次调用的数据进行第二次 JS .post 调用?

转载 作者:行者123 更新时间:2023-12-02 18:10:56 25 4
gpt4 key购买 nike

因此,我有一个 JS .post 调用,它通过 Controller 操作向表中添加一行。然后,它将该行的主键返回到 View 。我需要将该整数插入到我需要对不同 Controller 进行的第二个 .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');

这些是被调用的 Controller 操作:

        //
// 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 调用创建一个成功处理程序(其中 key 可用),并在该成功处理程序中进行第二个 ajax 调用,然后可以传递该 key 。

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

关于c# - 如何使用上一次调用的数据进行第二次 JS .post 调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19720912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com