D-W
D-W

Reputation: 5341

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?

Upvotes: 0

Views: 87

Answers (3)

Kartikeya Khosla
Kartikeya Khosla

Reputation: 18873

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);

Upvotes: 3

Nil
Nil

Reputation: 101

Change your Controller return type:

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

Upvotes: 0

TechGuy
TechGuy

Reputation: 4560

You missed the data type of the ajax call

dataType : 'json',

Upvotes: 0

Related Questions