I've commented out the rest of the javascript leaving the barebones behind. This method still doesn't work but at least i know its nothing else thats causing it to fail.
I've wired a simple button to the function that makes the ajax request and it still calls the error method. :( I'm lost! I have no idea why it doesn't work. Here's the complete code listing
ruddj16
Member
59 Points
92 Posts
Re: calling controller method using ajax
Jun 25, 2012 01:20 PM|LINK
Still doesn't work.
i do have other methods within that file. Could these be causing an issue?
ruddj16
Member
59 Points
92 Posts
Re: calling controller method using ajax
Jun 25, 2012 01:24 PM|LINK
I've commented out the rest of the javascript leaving the barebones behind. This method still doesn't work but at least i know its nothing else thats causing it to fail.
DannyMualim
Member
260 Points
77 Posts
Re: calling controller method using ajax
Jun 25, 2012 01:24 PM|LINK
There will be a problem if it has duplicate method name. if not, it should work as in my PC.
Danny
ruddj16
Member
59 Points
92 Posts
Re: calling controller method using ajax
Jun 25, 2012 01:26 PM|LINK
No duplicated method names. If I change the @Url to be /controller/action that invokes the controller but I still get a server error.
DannyMualim
Member
260 Points
77 Posts
Re: calling controller method using ajax
Jun 25, 2012 01:29 PM|LINK
Question, do you use Routes.MapRoutes? if yes, is the default route.map [query below] sit on global.asax.cs? If not, that causes the problem.
routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults );Cheers,
Danny
ruddj16
Member
59 Points
92 Posts
Re: calling controller method using ajax
Jun 25, 2012 01:30 PM|LINK
Danny,
this is what i've got in my routes.Map...
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Logon", action = "Index", id = UrlParameter.Optional } // Parameter defaults ); }Could it be the fact that I'm calling it from a ajax form submit method?
ruddj16
Member
59 Points
92 Posts
Re: calling controller method using ajax
Jun 25, 2012 01:35 PM|LINK
I've wired a simple button to the function that makes the ajax request and it still calls the error method. :( I'm lost! I have no idea why it doesn't work. Here's the complete code listing
Html page
@model SpecialsApplication.Models.LogOnModel <div id="login"> Login to your account. <div id="logindialog"> @using (Html.BeginForm("Login", "Logon", FormMethod.Post, new { id = "searchForm" })) { <div class="infopanel">Username:@Html.TextBoxFor(model => model.UserName) <!--Username:<input type="text" name="uname" size="35"/>--></div> <div class="infopanel">Password:@Html.TextBoxFor(model => model.Password)</div> <div class="infopanel"> <input type="submit" value="Submit" class="submit" id="loginBtn"/> </div> } <button id="btnTestConnection" onclick="return false;">Test connection</button> </div> </div>And the javascript file
$(document).ready(function () { $("button[id$='btnTestConnection']").click(function () { updatemyMenu(); }); function updatemyMenu() { window.alert("hello world"); $.ajax({ type: "GET", url: '@Url.Action("Temp", "Logon")', contentType: "application/json; charset=utf-8", success: function (returndata) { if (returndata.ok) { //do something with returndata.mydat window.alert(' everything is okay : ' + returndata.data.password); } else { window.alert(' everything is okay : ' + returndata.data.password); } }, error: function (xhr, desc, error) { window.alert(xhr); window.alert('description' + desc); window.alert('error' + error); } }); } });and the controller
public JsonResult Temp() { string h = "Hello world"; return new JsonResult() { Data = null }; }DannyMualim
Member
260 Points
77 Posts
Re: calling controller method using ajax
Jun 25, 2012 01:51 PM|LINK
Hmmm no idea what's happening here. but this might work.
Ajax Function:
function updatemyMenu() { window.alert("hello world"); $.ajax({ type: "POST", url: '@Url.Action("Temp", "Logon")', contentType: "application/json; charset=utf-8", dataType: "json", success: function (returndata) { if (returndata.ok) { //do something with returndata.mydat window.alert(' everything is okay : ' + returndata.data.password); } else { window.alert(' everything is okay : ' + returndata.data.password); } }, error: function (xhr, desc, error) { window.alert(xhr); window.alert('description' + desc); window.alert('error' + error); } }); }And in the controller, please add [HttpPost] for that method
Danny Mualim
ruddj16
Member
59 Points
92 Posts
Re: calling controller method using ajax
Jun 25, 2012 01:54 PM|LINK
Sadly I'm getting the same result. I've attempted to debug it but that doesn't return much either. :(
Unnics
Member
394 Points
93 Posts
Re: calling controller method using ajax
Jun 25, 2012 03:05 PM|LINK
Hi,
Try this.
$(document).ready(function () { function displayMenu() { window.alert("hello world"); $.ajax({ type: "POST", url: 'Menu/Index', datatype: "JSON", contentType: "application/json; charset=utf-8", success: function (returndata) { if (returndata.ok) { //do something with returndata.mydat window.alert(' everything is okay : ' + returndata.data.password); } else { window.alert(' everything is okay : ' + returndata.data.password); } }, error: handleError }); } function handleError(a, desc, error) { window.alert(error); }Hope this helps