Home > Backend Development > C++ > How to Make Effective Ajax Calls to an ASP.NET MVC Controller?

How to Make Effective Ajax Calls to an ASP.NET MVC Controller?

Susan Sarandon
Release: 2025-01-15 12:33:50
Original
582 people have browsed it

How to Make Effective Ajax Calls to an ASP.NET MVC Controller?

A complete guide to making Ajax requests to ASP.NET MVC controllers using jQuery

This article will guide you on how to use jQuery to initiate Ajax requests to ASP.NET MVC controllers, including the implementation of controllers and views, as well as client-server communication.

Controller operation

Your controller should define actions for handling Ajax requests. In the following example, FirstAjax is an operation that responds with data in JSON format:

public ActionResult FirstAjax()
{
    return Json("chamara", JsonRequestBehavior.AllowGet);
}
Copy after login

View

The

view initializes jQuery and makes an Ajax request to the FirstAjax action. The request includes a success handler that displays the response in an alert box:

$(document).ready(function () {
    var serviceURL = '/AjaxTest/FirstAjax';

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

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

    function errorFunc() {
        alert('error');
    }
});
Copy after login

Improvement plan

Note that the data attribute in the original Ajax request causes problems because your controller does not expect any parameters. Removing this attribute and using the @Url.Action method will solve the problem:

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

For POST requests that include data, you need to modify the controller action to accept the parameters and modify the Ajax request to include the data:

// 控制器
[HttpPost]
public ActionResult FirstAjax(string a)
{
    return Json("chamara", JsonRequestBehavior.AllowGet);
}

// 视图
$.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
});
Copy after login

The above is the detailed content of How to Make Effective Ajax Calls to an ASP.NET MVC Controller?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template