Calling controller method (ASP MVC3) method from a

2019-09-06 20:03发布

问题:

I am using the following syntax to make a call to controller method from ASP page.

$.ajax({
             url: 'ControllerName/MethodName',
             type: 'POST',
             contentType: 'application/json;',
             data: JSON.stringify({ param: param1}),
             success: function () {
                 alert("Success!!!");
             },
             error: function () {
                 alert("Failed!!!");
             }
         });

I have two ASP pages (views), both having same controller. If I call above method from first page, controller method gets called successfully. But if call same method from second page I get alert message "Failed". Also I tried using GET type, tried with other controller methods and all. Nothing will be called from second view. can anyone help me what can be problem? I am new to MVC.

回答1:

Since your ajax is expecting result of JSON data from your Controller method do you have return Json(data, JsonRequestBehavior.AllowGet)?



回答2:

Try change content type to:

contentType: 'application/json; charset=utf-8'

or/and specify url using mvc helper like:

url: @Url.Action("action"),

Works in my example. Hope it will help.