简体   繁体   中英

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 . However, the record is inserted in 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 :

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

Any comment or suggestion is appreciated

UPDATE

Json(term).Data contents:

-       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.

Try adding a Try { } Catch { } to the action and I reckon there will be an exception caught from the SaveTerm method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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