Home >Backend Development >C++ >How to Make Simple AJAX Calls to an ASP.NET MVC Controller?
Simplified AJAX Calls in ASP.NET MVC
Enhance your ASP.NET MVC applications with AJAX for seamless controller interaction without full page refreshes. This guide outlines a straightforward approach to making basic AJAX calls.
Controller Action:
Create a controller action method specifically designed for AJAX requests:
<code class="language-csharp">public class AjaxTestController : Controller { public ActionResult FirstAjax() { return Json("chamara", JsonRequestBehavior.AllowGet); } }</code>
View Script:
Incorporate the necessary JavaScript libraries and implement the AJAX call within your view:
<code class="language-javascript">$(document).ready(function() { const serviceURL = '@Url.Action("FirstAjax", "AjaxTest")'; $.ajax({ type: "POST", url: serviceURL, dataType: "json", success: function(data, status) { alert(data); }, error: function() { alert('error'); } }); });</code>
Successful Response:
Upon successful execution, the success
function displays the received JSON data in an alert box.
Debugging Tips:
Should you encounter problems, verify the following:
data
attribute is omitted from the AJAX call (as shown in the corrected example).[HttpPost]
attribute.The above is the detailed content of How to Make Simple AJAX Calls to an ASP.NET MVC Controller?. For more information, please follow other related articles on the PHP Chinese website!