Making an Ajax Call to a Controller in ASP.NET MVC
Problem:
When attempting to retrieve data from a controller via Ajax, a JavaScript alert is not firing, despite the controller method returning the expected result.
Solution:
<code class="javascript">$.ajax({ url: '@Url.Action("FirstAjax", "AjaxTest")', contentType: "application/json; charset=utf-8", dataType: "json", success: successFunc, error: errorFunc });</code>
Updated Code with POST Data (Optional):
If the controller method requires POST data, it can be passed through the data attribute:
<code class="javascript">$.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 });</code>
The above is the detailed content of How to Resolve JavaScript Alert Not Firing in AJAX Call to ASP.NET MVC Controller?. For more information, please follow other related articles on the PHP Chinese website!