Controller code here
- [HttpGet]
- public JsonResult GetAjaxData(string newCode) {
- return Json(new {
- Status = "Ajax called succesfully", Message = "Message show succesfully"
- }, JsonRequestBehavior.AllowGet);
- }
View Code Here
- @ {
- ViewBag.Title = "Index";
- } < script src = "~/Scripts/jquery-1.8.2.js" > < /script>
- <script>
- function AjaxCall()
- {
- $.ajax({
- async: false,
- url: $('#ajaxURL').val(),
- type: 'GET',
- cache: false,
- data: {
- newCode : "Put the data here"
- },
- success: function (result) {
- if(result.Status !=''){
- alert(result.Status);
- }
- else{
- alert('Ajax called failed');
- }
- },
- error: function (result, textStatus, errorThrown) {
- alert(errorThrown);
- }
- })
- }
- </script > < div > < button value = "Ajax Called"
- id = "btncalled"
- onclick = "AjaxCall()" > Ajax Called < /button>
- </div > @Html.Hidden("ajaxURL", Url.Action("GetAjaxData", "Demo"))
App_Start-> route config.cs code here
- public static void RegisterRoutes(RouteCollection routes) {
- routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
- routes.MapRoute(
- name: "Default",
- url: "{controller}/{action}/{id}",
- defaults: new {
- controller = "Demo", action = "Index", id = UrlParameter.Optional
- });
- }
For better detail see the image which i have attached
Thanks