如何使用先前调用的数据进行第二次JS .post调用?

时间:2013-11-01 04:41:18

标签: c# javascript json asp.net-mvc-3

所以,我有一个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);
    }

1 个答案:

答案 0 :(得分:2)

您为第一个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');