suman
suman

Reputation: 343

Ajax json post is not working

I need to pass a json data to the controller. hence i have created a ajax post. But it is not calling the action method.

function DeleteRow(postData) {
$.ajax({
    url: '@Url.Action("DeleteGridRow","Project")',
    type: 'POST',
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    async: false,
    data: JSON.stringify(postData),
    success: function (data) {

    }
});

}

My Actionmethod

 [HttpPost]
    public JsonResult DeleteGridRow(string postData)
    {
        return Json(null);
    }

Please help

Upvotes: 1

Views: 1988

Answers (1)

Huske
Huske

Reputation: 9296

If you have separated your javascript file from your cshtml or vbhtml page, then this is not going to work. Your URL would be interpreted wrongly. You should pass URL where you are submitting to your function DeleteRow. Something like this:

$("#myForm").submit(function() {
    var url = $(this).attr("action");
    var data = Use your method to collect data or $(this).serialize();

    DeleteRow(url, data);
});

function DeleteRow(url, postData) {  
    $.ajax({  
        url: url,  
        type: 'POST',  
        dataType: "json",  
        contentType: "application/json; charset=utf-8",  
        async: false,  
        data: JSON.stringify(postData),  
        success: function (data) {  

    }  
});

Something like this should work.

Upvotes: 2

Related Questions