简体   繁体   English

JsonResult之后未调用Ajax成功

[英]Ajax success is not called after JsonResult

I am trying to add the record to DB using Ajax and get the data back from JsonResult if success, in order to call the function further, but somehow always land in the error: parseerror . 我试图使用Ajax将记录添加到DB,如果成功,则从JsonResult取回数据,以便进一步调用该函数,但总会出现错误: parseerror However, the record is inserted in DB. 但是,该记录已插入DB中。

Here is my post method: 这是我的发布方法:

$("body").on("click", "#btnAdd", function () {
        var txtTermName = $("#txtTermsName");
        var txtAlternativeTerms = $("#txtAlternativeTerms");
        $.ajax({
            type: "POST",
            url: "/Administration/AddTerm",
            data: '{name: "' + txtTermName.val() + '", alternatives: "' + txtAlternativeTerms.val() + '" }',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (r) {
                var row = $("#tblTerms tr:last-child");
                if ($("#tblTerms tr:last-child span").eq(0).html() != " ") {
                    row = row.clone();
                }
                AppendRow(row, r.Name, r.Alternatives);
                txtTermName.val("");
                txtAlternativeTerms.val("");
            },
            error: function(textStatus, errorThrown) { 
                alert("Error: " + errorThrown); 
            }     
        });
    });

And my controller JsonResult : 和我的控制器JsonResult

[HttpPost]
public JsonResult AddTerm(Term term)
{
    this.SaveTerm(term);
    return Json(term);
}

Any comment or suggestion is appreciated 任何意见或建议表示赞赏

UPDATE UPDATE

Json(term).Data contents: Json(term).Data内容:

-       Json(term).Data {Models.Term}   object {Models.Term}
+       ChangedBy   
        Description null    string
        ID  27  int
        Inactive    false   bool
        Name    "sdfgdsgf"  string
        SynonymsList    "sdfgdsgfdsgsdgf"   string
+       Updated {09.08.2018 10:00:50}   System.DateTime

Looks like an exception is being called somewhere after your database save call (I take it the SaveTerm method does more than just save the item?) resulting in an error page being returned instead of JSON - hence the parse error. 看起来在您的数据库保存调用之后某个地方正在调用异常(我认为SaveTerm方法不只是保存项目吗?),导致返回错误页面而不是JSON-因此是解析错误。

Try adding a Try { } Catch { } to the action and I reckon there will be an exception caught from the SaveTerm method. 尝试将Try {} Catch {}添加到操作中,我认为将从SaveTerm方法中捕获异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM