Ajax Call不调用Controller Action Method

时间:2016-12-16 20:34:24

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

我无法对返回json对象的控制器操作方法进行ajax调用。 另外,我想将整数CheckID传递给方法。

非常感谢任何帮助。 在此先感谢!

***View***

<script type="text/javascript">

function showCheckImage(e) {
    e.preventDefault();

    var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
    var CheckID = dataItem.ID;

    $.ajax({
        url: '@Url.Action("GetDeferredCheckImage", "Customer")',
        type: 'POST',
        data: {deferredCheckID: CheckID },
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',

        success: function(result) {
            //var imageObj = result;
            alert('Loaded Image Object!');
        },
        error: function (result) {
            alert('Error occurred while loading the image object.');
        }
    });
}

**Controller Method**

[HttpPost]
    public JsonResult GetDeferredCheckImage(int deferredCheckID)
    {
        try
        {
            QCEventLogger.Log($"Gathering deferred check image for check ID: {deferredCheckID}", LogType.Default);
            var response = new AjaxGetDeferredCheckImageViewModel(deferredCheckID);
            QCEventLogger.Log($"Result of service call to gather deferred check image.  check ID: {deferredCheckID}. Success: {response.Success}", LogType.Default);

            var DeferredCheckImageObject = response.ImageCheckObject.DeferredCheckImages.FirstOrDefault();

            return Json(DeferredCheckImageObject, JsonRequestBehavior.AllowGet);
        }
        catch (Exception ex)
        {
            var failureResponse = new AjaxGetDeferredCheckImageViewModel() { Success = false };
            QCErrorLogger.Log($"Failure trying to gather deferred check image for check ID: {deferredCheckID}", ex);
            return Json(failureResponse, JsonRequestBehavior.AllowGet);                
        }
    }

1 个答案:

答案 0 :(得分:0)

你的ajax电话可能是这样的,

 $.ajax({
        url: '@Url.Action("GetDeferredCheckImage", "Customer")',
        type: 'POST',
        data: { deferredCheckID: CheckID },
        dataType: 'json',
        success: function (result) {
            //var imageObj = result;
            alert('Loaded Image Object!');
        },
        error: function (result) {
            alert('Error occurred while loading the image object.');
        }
    });