从Ajax调用控制器方法(ASP MVC3)方法不起作用 [英] Calling controller method (ASP MVC3) method from ajax doesn't work

查看:53
本文介绍了从Ajax调用控制器方法(ASP MVC3)方法不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下语法从ASP页面调用控制器方法.

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!!!");
             }
         });

我有两个ASP页面(视图),两个页面都有相同的控制器.如果我从第一页调用上述方法,则控制器方法将成功调用.但是,如果从第二页调用相同的方法,则会收到警报消息失败".我也尝试使用GET类型,尝试使用其他控制器方法和所有方法.从第二个角度来看,什么也不会被调用.谁能帮助我什么问题?我是MVC的新手.

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.

推荐答案

尝试将内容类型更改为:

Try change content type to:

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

或/并使用mvc辅助程序指定网址,例如:

or/and specify url using mvc helper like:

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

在我的示例中有效.希望对您有所帮助.

Works in my example. Hope it will help.

这篇关于从Ajax调用控制器方法(ASP MVC3)方法不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆