简体   繁体   中英

Jquery Ajax error called

Im calling my ajax on the following

function LoadAudit(value) {

    $.ajax({
        url: '/Account/GetAuditRecord/' + value,
        success: function (data) {
            $("#htmlResult").val(data.html);
        },
        error:function(data) {
            alert('error');
        },
    });
};

Which calls my controller

public JsonResult GetAuditRecord(string Id)
         {
              string html =_auditLogService.FindAllByAccount().Single(a => a.Id == Id).Comments ;
             return Json(new { html = result});
         }

Which works (data is looked up and ready to pass back), but error is being fired in jquery, when success should be, what have I missed?

Add dataType as 'JSON' in ajax call as shown:

$.ajax({
     url: '/Account/GetAuditRecord/' + value,
     dataType : 'JSON',
     success: function (data) {
         $("#htmlResult").val(data.html);
     },
     error:function(data) {
         alert('error');
     },
});

and change jsonresult in controller action as shown:

return Json(new { html = result }, JsonRequestBehavior.AllowGet);

您错过了ajax调用的数据类型

dataType : 'json',

更改您的Controller返回类型:

return Json( new { html = result }, JsonRequestBehavior.AllowGet );

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