Ajax调用永远不会返回

时间:2015-05-08 08:31:31

标签: jquery asp.net ajax asp.net-mvc asp.net-mvc-4

我有以下ajax调用:

$.ajax({
            type: "POST",
            url: urlToGetRules,
            data: { ruleName: ruleName},
        })
             .success(function (data) {
                 document.location.href = "/StudentRules/GetAllStudentRules?productId=test";
             })
             .fail(function (xhr) {
                 alert("Something went wrong!!!")
                 console.log(xhr.responseText);
             });

在我的控制器中,我正在DocDb中创建一个文档,如下所示:

[HttpPost]
public ActionResult UpsertDoc(string ruleName)
{
            StudentRule studentRule = new StudentRule() { Id = Guid.NewGuid().ToString(), StudentId = "test", Name = ruleName, RuleType = "Allow all updates", StartDate = DateTime.UtcNow.ToString() };
            _updateScheduleRulesManager.UpsertScheduleRule(studentRule);

            return Json(new { success = true });
}

我们的想法是返回一个页面,在用户在“添加规则”页面中创建新规则后,我会列出所有规则。

上面的代码执行正常,我可以在Docdb中看到预期的文档,但是在开发人员工具中此调用的状态显示为' !!! 成功中的代码永远不会执行。

有人可以在这里指导我吗?

提前致谢。

1 个答案:

答案 0 :(得分:0)

我认为有问题你没有在请求中传递productid在ajax调用中检查这个

data: { ruleName: ruleName},

没有productid,因为你的方法productid是参数

public ActionResult UpsertDoc(string ruleName, string productId)

您的数据元素应该是并添加数据类型

data: { ruleName: 'name of rule', productId: 'productid' }, 
datatype : 'json'