calling controller method using ajaxhttp://forums.asp.net/t/1817945.aspx/1?calling+controller+method+using+ajaxTue, 26 Jun 2012 04:30:30 -040018179455039552http://forums.asp.net/p/1817945/5039552.aspx/1?calling+controller+method+using+ajaxcalling controller method using ajax <p>Hi guys,</p> <p>I've got an ajax method that should work but doesn't</p> <pre class="prettyprint">$(document).ready(function () { function displayMenu() { window.alert(&quot;hello world&quot;); $.ajax({ type: &quot;POST&quot;, url: '&lt;%=Url.Action(&quot;Index&quot;, &quot;Menu&quot;)%&gt;', datatype: &quot;JSON&quot;, contentType: &quot;application/json; charset=utf-8&quot;, error: handleError, success: function (returndata) { if (returndata.ok) { //do something with returndata.mydat window.alert(' everything is okay : ' &#43; returndata.data.password); } else { window.alert(' everything is okay : ' &#43; returndata.data.password); } } }); } function handleError(a, desc, error) { window.alert(error); } ...more js</pre> <p>Now, its been called correctly since the messages appear but the error is printing out &quot;Error: Invalid argument&quot;. i don't see anything wrong with my ajax call to my controller.</p> 2012-06-25T12:24:45-04:005039561http://forums.asp.net/p/1817945/5039561.aspx/1?Re+calling+controller+method+using+ajaxRe: calling controller method using ajax <pre class="prettyprint">instead of url: '&lt;%=Url.Action(&quot;Index&quot;, &quot;Menu&quot;)%&gt;',</pre> <pre class="prettyprint"><span class="pun">just try url:/ControllerName/</span></pre> 2012-06-25T12:34:11-04:005039564http://forums.asp.net/p/1817945/5039564.aspx/1?Re+calling+controller+method+using+ajaxRe: calling controller method using ajax <p>How do I call a method on the controller? I've actually noticed the url.action is mvc2 not mvc3 so i've updated it to read</p> <pre class="prettyprint">function displayMenu() { window.alert(&quot;hello world&quot;); $.ajax({ type: &quot;POST&quot;, url: '@Url.Action(&quot;Temp&quot;)', datatype: &quot;JSON&quot;, contentType: &quot;application/json; charset=utf-8&quot;, success: function (returndata) { if (returndata.ok) { //do something with returndata.mydat window.alert(' everything is okay : ' &#43; returndata.data.password); } else { window.alert(' everything is okay : ' &#43; returndata.data.password); } }, error: function (xhr, desc, error) { window.alert('description' &#43; desc); window.alert('error' &#43; error); } }); }</pre> <p>that still doesn't work. I've created a new post method in my controller and named it Temp to see if it was because of the call being to another controller. It still doesn't hit the break point!</p> 2012-06-25T12:35:52-04:005039571http://forums.asp.net/p/1817945/5039571.aspx/1?Re+calling+controller+method+using+ajaxRe: calling controller method using ajax <p>Hi Ruddj16,</p> <p>Are you using [HttpPost] for the Action like below query? If not, you should change the ajax type to &quot;Get&quot; [second query]</p> <pre class="prettyprint">[HttpPost] public ActionResult Index() { ... }</pre> <p>Javascript</p> <pre class="prettyprint"><span class="pln">&#36;</span><span class="pun">.</span><span class="pln">ajax</span><span class="pun">({</span><span class="pln"><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <strong>type</strong></span><strong><span class="pun">:</span><span class="pln"> </span><span class="str">"Get"</span></strong><span class="pun">,</span><span class="pln"><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url</span><span class="pun">:</span><span class="pln"> </span><span class="str">'&lt;%=Url.Action("Index", "Menu")%&gt;'</span><span class="pun">,</span><span class="pln"><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; datatype</span><span class="pun">:</span><span class="pln"> </span><span class="str">"JSON"</span><span class="pun">,</span><span class="pln"><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contentType</span><span class="pun">:</span><span class="pln"> </span><span class="str">"application/json; charset=utf-8"</span><span class="pun">,</span><span class="pln"><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; error</span><span class="pun">:</span><span class="pln"> handleError</span><span class="pun">,</span><span class="pln"><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">function</span><span class="pln"> </span><span class="pun">(</span><span class="pln">returndata</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">returndata</span><span class="pun">.</span><span class="pln">ok</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="com">//do something with returndata.mydat</span><span class="pln"><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; window</span><span class="pun">.</span><span class="pln">alert</span><span class="pun">(</span><span class="str">' everything is okay : '</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> returndata</span><span class="pun">.</span><span class="pln">data</span><span class="pun">.</span><span class="pln">password</span><span class="pun">);</span><span class="pln"><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="pun">}</span><span class="pln"> </span><span class="kwd">else</span><span class="pln"> </span><span class="pun">{</span><span class="pln"><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; window</span><span class="pun">.</span><span class="pln">alert</span><span class="pun">(</span><span class="str">' everything is okay : '</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> returndata</span><span class="pun">.</span><span class="pln">data</span><span class="pun">.</span><span class="pln">password</span><span class="pun">);</span><span class="pln"><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="pun">}</span><span class="pln"><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span class="pun">}</span><span class="pln"><br />&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="pun">});</span><span class="pln"><br /></span></pre> <p>Hopefully, this answers your question.</p> <p>Danny Mualim<br> <br> </p> 2012-06-25T12:41:02-04:005039578http://forums.asp.net/p/1817945/5039578.aspx/1?Re+calling+controller+method+using+ajaxRe: calling controller method using ajax <p>Heres the controller with the dummy method.</p> <pre class="prettyprint">public JsonResult Temp() { string h = &quot;Hello world&quot;; return new JsonResult() { Data = null }; }</pre> <p>Now when I run it, I get an internal server error value from my alerts.</p> 2012-06-25T12:44:55-04:005039590http://forums.asp.net/p/1817945/5039590.aspx/1?Re+calling+controller+method+using+ajaxRe: calling controller method using ajax <p>Hmmm... are you using the ajax query same as the one that you metion? or different one? if different, could you post it here?</p> <p>Danny</p> 2012-06-25T12:51:28-04:005039592http://forums.asp.net/p/1817945/5039592.aspx/1?Re+calling+controller+method+using+ajaxRe: calling controller method using ajax <p>here's the ajax call</p> <pre class="prettyprint">function displayMenu() { window.alert(&quot;hello world&quot;); $.ajax({ type: &quot;POST&quot;, url: '@Url.Action(&quot;Temp&quot;, &quot;Logon&quot;)', datatype: &quot;JSON&quot;, contentType: &quot;application/json; charset=utf-8&quot;, success: function (returndata) { if (returndata.ok) { //do something with returndata.mydat window.alert(' everything is okay : ' &#43; returndata.data.password); } else { window.alert(' everything is okay : ' &#43; returndata.data.password); } }, error: function (xhr, desc, error) { window.alert('description' &#43; desc); window.alert('error' &#43; error); } }); }</pre> <p></p> 2012-06-25T12:52:30-04:005039597http://forums.asp.net/p/1817945/5039597.aspx/1?Re+calling+controller+method+using+ajaxRe: calling controller method using ajax <p>Could you try below query? I changed the type from &quot;POST&quot; to &quot;GET&quot;</p> <pre class="prettyprint">function displayMenu() { window.alert(&quot;hello world&quot;); $.ajax({ type: &quot;GET&quot;, url: '@Url.Action(&quot;Temp&quot;, &quot;Logon&quot;)', datatype: &quot;JSON&quot;, contentType: &quot;application/json; charset=utf-8&quot;, success: function (returndata) { if (returndata.ok) { //do something with returndata.mydat window.alert(' everything is okay : ' &#43; returndata.data.password); } else { window.alert(' everything is okay : ' &#43; returndata.data.password); } }, error: function (xhr, desc, error) { window.alert('description' &#43; desc); window.alert('error' &#43; error); } }); }</pre> <p><br> Cheers,</p> <p>Danny</p> 2012-06-25T12:54:49-04:005039606http://forums.asp.net/p/1817945/5039606.aspx/1?Re+calling+controller+method+using+ajaxRe: calling controller method using ajax <p>thanks Danny,</p> <p>&nbsp;</p> <p>Sadly it produces the same result.</p> <p>Would more code be helpful?</p> 2012-06-25T12:57:29-04:005039632http://forums.asp.net/p/1817945/5039632.aspx/1?Re+calling+controller+method+using+ajaxRe: calling controller method using ajax <p>Ok, I just did experiment. please removed <strong><span class="pln">datatype</span><span class="pun">:</span><span class="pln"> </span><span class="str">&quot;JSON&quot;.</span></strong></p> <pre class="prettyprint">function displayMenu() { window.alert(&quot;hello world&quot;); $.ajax({ type: &quot;GET&quot;, url: '@Url.Action(&quot;Temp&quot;, &quot;Logon&quot;)', //datatype: &quot;JSON&quot;, contentType: &quot;application/json; charset=utf-8&quot;, success: function (returndata) { if (returndata.ok) { //do something with returndata.mydat window.alert(' everything is okay : ' &#43; returndata.data.password); } else { window.alert(' everything is okay : ' &#43; returndata.data.password); } }, error: function (xhr, desc, error) { window.alert('description' &#43; desc); window.alert('error' &#43; error); } }); }</pre> <p>Hopefully, this works at your PC.</p> <p>Danny Mualim</p> 2012-06-25T13:13:05-04:005039643http://forums.asp.net/p/1817945/5039643.aspx/1?Re+calling+controller+method+using+ajaxRe: calling controller method using ajax <p>Still doesn't work.</p> <p>i do have other methods within that file. Could these be causing an issue?</p> 2012-06-25T13:20:58-04:005039648http://forums.asp.net/p/1817945/5039648.aspx/1?Re+calling+controller+method+using+ajaxRe: calling controller method using ajax <p>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.</p> 2012-06-25T13:24:12-04:005039650http://forums.asp.net/p/1817945/5039650.aspx/1?Re+calling+controller+method+using+ajaxRe: calling controller method using ajax <p>There will be a problem if it has duplicate method name. if not, it should work as in my PC.</p> <p>Danny</p> 2012-06-25T13:24:59-04:005039653http://forums.asp.net/p/1817945/5039653.aspx/1?Re+calling+controller+method+using+ajaxRe: calling controller method using ajax <p>No duplicated method names. If I change the @Url to be /controller/action that invokes the controller but I still get a server error.</p> 2012-06-25T13:26:04-04:005039657http://forums.asp.net/p/1817945/5039657.aspx/1?Re+calling+controller+method+using+ajaxRe: calling controller method using ajax <p>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.</p> <pre class="prettyprint">routes.MapRoute( &quot;Default&quot;, // Route name &quot;{controller}/{action}/{id}&quot;, // URL with parameters new { controller = &quot;Home&quot;, action = &quot;Index&quot;, id = UrlParameter.Optional } // Parameter defaults );</pre> <p></p> <p>Cheers,</p> <p>Danny</p> 2012-06-25T13:29:16-04:005039663http://forums.asp.net/p/1817945/5039663.aspx/1?Re+calling+controller+method+using+ajaxRe: calling controller method using ajax <p>Danny,</p> <p>&nbsp;</p> <p>this is what i've got in my routes.Map...</p> <p>&nbsp;</p> <pre class="prettyprint">public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute(&quot;{resource}.axd/{*pathInfo}&quot;); routes.MapRoute( &quot;Default&quot;, // Route name &quot;{controller}/{action}/{id}&quot;, // URL with parameters new { controller = &quot;Logon&quot;, action = &quot;Index&quot;, id = UrlParameter.Optional } // Parameter defaults ); }</pre> <p>Could it be the fact that I'm calling it from a ajax form submit method?</p> 2012-06-25T13:30:49-04:005039670http://forums.asp.net/p/1817945/5039670.aspx/1?Re+calling+controller+method+using+ajaxRe: calling controller method using ajax <p>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</p> <p>Html page</p> <pre class="prettyprint">@model SpecialsApplication.Models.LogOnModel &lt;div id=&quot;login&quot;&gt; Login to your account. &lt;div id=&quot;logindialog&quot;&gt; @using (Html.BeginForm(&quot;Login&quot;, &quot;Logon&quot;, FormMethod.Post, new { id = &quot;searchForm&quot; })) { &lt;div class=&quot;infopanel&quot;&gt;Username:@Html.TextBoxFor(model =&gt; model.UserName) &lt;!--Username:&lt;input type=&quot;text&quot; name=&quot;uname&quot; size=&quot;35&quot;/&gt;--&gt;&lt;/div&gt; &lt;div class=&quot;infopanel&quot;&gt;Password:@Html.TextBoxFor(model =&gt; model.Password)&lt;/div&gt; &lt;div class=&quot;infopanel&quot;&gt; &lt;input type=&quot;submit&quot; value=&quot;Submit&quot; class=&quot;submit&quot; id=&quot;loginBtn&quot;/&gt; &lt;/div&gt; } &lt;button id=&quot;btnTestConnection&quot; onclick=&quot;return false;&quot;&gt;Test connection&lt;/button&gt; &lt;/div&gt; &lt;/div&gt;</pre> <p>And the javascript file</p> <pre class="prettyprint">&#36;(document).ready(function () { &#36;("button[id&#36;='btnTestConnection']").click(function () { updatemyMenu(); }); function updatemyMenu() { window.alert("hello world"); &#36;.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); } }); } });</pre> <p>and the controller<br /><br /></p> <pre class="prettyprint">public JsonResult Temp() { string h = "Hello world"; return new JsonResult() { Data = null }; } </pre> <p></p> 2012-06-25T13:35:47-04:005039691http://forums.asp.net/p/1817945/5039691.aspx/1?Re+calling+controller+method+using+ajaxRe: calling controller method using ajax <p>Hmmm no idea what's happening here. but this might work.</p> <p>Ajax Function:</p> <pre class="prettyprint">function updatemyMenu() { window.alert(&quot;hello world&quot;); $.ajax({ type: &quot;POST&quot;, url: '@Url.Action(&quot;Temp&quot;, &quot;Logon&quot;)', contentType: &quot;application/json; charset=utf-8&quot;, dataType: &quot;json&quot;, success: function (returndata) { if (returndata.ok) { //do something with returndata.mydat window.alert(' everything is okay : ' &#43; returndata.data.password); } else { window.alert(' everything is okay : ' &#43; returndata.data.password); } }, error: function (xhr, desc, error) { window.alert(xhr); window.alert('description' &#43; desc); window.alert('error' &#43; error); } }); }</pre> <p>And in the controller, please add [HttpPost] for that method</p> <pre class="prettyprint">[HttpPost]<br />public JsonResult Temp() { string h = "Hello world"; return new JsonResult() { Data = null }; }</pre> <p>Danny Mualim<br> <br> </p> <p></p> <p><br> <br> </p> 2012-06-25T13:51:59-04:005039695http://forums.asp.net/p/1817945/5039695.aspx/1?Re+calling+controller+method+using+ajaxRe: calling controller method using ajax <p>Sadly I'm getting the same result. I've attempted to debug it but that doesn't return much either. :(</p> 2012-06-25T13:54:53-04:005039791http://forums.asp.net/p/1817945/5039791.aspx/1?Re+calling+controller+method+using+ajaxRe: calling controller method using ajax <p>Hi,</p> <p>Try this.</p> <pre class="prettyprint">$(document).ready(function () { function displayMenu() { window.alert(&quot;hello world&quot;); $.ajax({ type: &quot;POST&quot;, url: 'Menu/Index', datatype: &quot;JSON&quot;, contentType: &quot;application/json; charset=utf-8&quot;, success: function (returndata) { if (returndata.ok) { //do something with returndata.mydat window.alert(' everything is okay : ' &#43; returndata.data.password); } else { window.alert(' everything is okay : ' &#43; returndata.data.password); } }, error: handleError }); } function handleError(a, desc, error) { window.alert(error); }</pre> <p>Hope this helps<br> <br> </p> 2012-06-25T15:05:44-04:00