Ajax调用不调用Controller Action Method

时间:2022-12-01 23:16:14

I'm not able to make ajax call to the controller action method which returns the json object. Also, I want to pass the integer-CheckID to the method.

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

Any help is highly appreciated. Thanks in Advance!

任何帮助都非常感谢。提前致谢!

***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 个解决方案

#1


0  

Your ajax call might be like this,

你的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.');
        }
    });

#1


0  

Your ajax call might be like this,

你的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.');
        }
    });