在asp.net mvc中对控制器进行简单的Ajax调用

cha*_*ara 104 c# asp.net asp.net-mvc jquery asp.net-mvc-2

我正在尝试开始使用ASP.NET MVC Ajax调用.

控制器:

public class AjaxTestController : Controller
{
    //
    // GET: /AjaxTest/
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult FirstAjax()
    {
        return Json("chamara", JsonRequestBehavior.AllowGet);
    }   
}
Run Code Online (Sandbox Code Playgroud)

视图:

<head runat="server">
    <title>FirstAjax</title>
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            var serviceURL = '/AjaxTest/FirstAjax';

            $.ajax({
                type: "POST",
                url: serviceURL,
                data: param = "",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: successFunc,
                error: errorFunc
            });

            function successFunc(data, status) {     
                alert(data);
            }

            function errorFunc() {
                alert('error');
            }
        });
    </script>
</head>
Run Code Online (Sandbox Code Playgroud)

我只需要使用控制器方法返回数据来打印警报.上面的代码只是在我的视图上打印"chamara".警报未触发.

UPDATE

我修改了我的控制器如下,它开始工作.我不清楚它为什么现在正在工作.有人请解释一下.参数"a"没有关联我添加它因为我不能添加两个方法具有相同的方法名称和参数.我认为这可能不是解决方案,但它的工作

public class AjaxTestController : Controller
    {
        //
        // GET: /AjaxTest/
        [HttpGet]
        public ActionResult FirstAjax()
        {
            return View();
        }

        [HttpPost]
        public ActionResult FirstAjax(string a)
        {
            return Json("chamara", JsonRequestBehavior.AllowGet);
        }
    }
Run Code Online (Sandbox Code Playgroud)

Dar*_*ren 49

删除数据属性,因为您不是POSTING服务器的任何内容(您的控制器不期望任何参数).

在您的AJAX方法中,您可以使用Razor和使用@Url.Action而不是静态字符串:

$.ajax({
    url: '@Url.Action("FirstAjax", "AjaxTest")',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: successFunc,
    error: errorFunc
});
Run Code Online (Sandbox Code Playgroud)

从您的更新:

$.ajax({
    type: "POST",
    url: '@Url.Action("FirstAjax", "AjaxTest")',
    contentType: "application/json; charset=utf-8",
    data: { a: "testing" },
    dataType: "json",
    success: function() { alert('Success'); },
    error: errorFunc
});
Run Code Online (Sandbox Code Playgroud)

  • @chamara - 抱歉,删除HttpPost(您没有向服务器发布任何内容,因此它将是一个冗余属性)并且控制器不会被命中.同样在我播种的AJAX函数中删除"type:POST". (2认同)

小智 22

完成更新后,

  1. 它首先使用默认的HttpGet请求调用FirstAjax操作并呈现空白的Html视图.(早先你没有)
  2. 稍后加载该视图的DOM元素时,您的Ajax调用将被触发并显示警报.

之前您只是将JSON返回给浏览器而不呈现任何HTML.现在它呈现了一个HTML视图,它可以获取您的JSON数据.

您不能直接呈现JSON的普通数据而不是HTML.


gdm*_*hon 13

使用Razor通过调用您的操作动态更改您的URL:

$.ajax({
    type: "POST",
    url: '@Url.Action("ActionName", "ControllerName")',
    contentType: "application/json; charset=utf-8",
    data: { data: "yourdata" },
    dataType: "json",
    success: function(recData) { alert('Success'); },
    error: function() { alert('A error'); }
});
Run Code Online (Sandbox Code Playgroud)


Haf*_*sad 8

如果你只需要在你的 Ajax 调用中点击 C# 方法,你只需要传递两个事件类型和 url(如果你的请求是 get),那么你只需要指定 url。请按照下面的代码它工作正常。

C# 代码:

    [HttpGet]
    public ActionResult FirstAjax()
    {
        return Json("chamara", JsonRequestBehavior.AllowGet);
    }   
Run Code Online (Sandbox Code Playgroud)

获取请求时的 Java 脚本代码

    $.ajax({
        url: 'home/FirstAjax',
        success: function(responce){ alert(responce.data)},
        error: function(responce){ alert(responce.data)}
    });
Run Code Online (Sandbox Code Playgroud)

如果发布请求以及 [HttpGet] 到 [HttpPost] 的 Java 脚本代码

        $.ajax({
            url: 'home/FirstAjax',
            type:'POST',
            success: function(responce){ alert(responce)},
            error: function(responce){ alert(responce)}
        });
Run Code Online (Sandbox Code Playgroud)

注意:如果您的 FirstAjax 与您的视图控制器在同一个控制器中,则 url 中不需要控制器名称。喜欢url: 'FirstAjax',


Lok*_*Ram 5

首先,不需要在一个页面中包含两个不同版本的 jquery 库,“1.9.1”或“2.0.0”足以使 ajax 调用工作。

这是您的控制器代码:

    public ActionResult Index()
    {
        return View();
    }

    public ActionResult FirstAjax(string a)
    {
        return Json("chamara", JsonRequestBehavior.AllowGet);
    }   
Run Code Online (Sandbox Code Playgroud)

这是您的视图的样子:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function () {
    var a = "Test";
    $.ajax({
        url: "../../Home/FirstAjax",
        type: "GET",
        data: { a : a },
        success: function (response) {
            alert(response);
        },
        error: function (response) {
            alert(response);
        }
    });
});
</script>
Run Code Online (Sandbox Code Playgroud)