I am working in asp.net webforms and jquery ajax since many years and now want to implement a project in asp.ner core using same front end technology jQuery Ajax.
2 Answers
jQuery implementation is same across multiple platforms. You can use similar code.
Example of jquery ajax:
$.ajax({
type: 'POST',
url: '/controller/Action'
data: { parameter1 : 'value', parameter2 : 'value' },
success: function (response) {
// code on successful completion
}
error: function (response) {
// code on failure
}
});
1 Comment
ma11achy
This should be the top, accepted answer. It's clear, concise and doesn't have any erroneous code.
You can apply inline data-ajax-* attributes as below:
Include jquery libraries:
@section Scripts {
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.unobtrusive-ajax.min.js"></script>
}
Apply data-ajax attributes to anchor element:
<!-- ajax content will load inside this div -->
<div id="data-div"></div>
<a href="#"
data-ajax="true"
data-ajax-mode="replace"
data-ajax-update="#data-div"
data-ajax-loading="#loading"
data-ajax-url="@Url.Page("Index", "ServerDate")">Run Ajax Request</a>
<!-- spinning loading indicator -->
<span id="loading" style="display:none;"> <i class="fas fa-spinner fa-spin"></i></span>
and the backend method:
public class IndexModel : PageModel
{
public void OnGet() { }
public ContentResult OnGetServerDate()
{
return Content($"{DateTime.Now}");
}
}
Same attributes can be applied to the forms as well.
See demos here: http://demo.ziyad.info/en/AjaxRequest
jQuery.