I've got an ajax method that should work but doesn't
$(document).ready(function () {
function displayMenu() {
window.alert("hello world");
$.ajax({
type: "POST",
url: '<%=Url.Action("Index", "Menu")%>',
datatype: "JSON",
contentType: "application/json; charset=utf-8",
error: handleError,
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);
}
}
});
}
function handleError(a, desc, error) {
window.alert(error);
}
...more js
Now, its been called correctly since the messages appear but the error is printing out "Error: Invalid argument". i don't see anything wrong with my ajax call to my controller.
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
function displayMenu() {
window.alert("hello world");
$.ajax({
type: "POST",
url: '@Url.Action("Temp")',
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: function (xhr, desc, error) {
window.alert('description' + desc);
window.alert('error' + error);
}
});
}
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!
ruddj16
Member
59 Points
92 Posts
calling controller method using ajax
Jun 25, 2012 12:24 PM|LINK
Hi guys,
I've got an ajax method that should work but doesn't
$(document).ready(function () { function displayMenu() { window.alert("hello world"); $.ajax({ type: "POST", url: '<%=Url.Action("Index", "Menu")%>', datatype: "JSON", contentType: "application/json; charset=utf-8", error: handleError, 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); } } }); } function handleError(a, desc, error) { window.alert(error); } ...more jsNow, its been called correctly since the messages appear but the error is printing out "Error: Invalid argument". i don't see anything wrong with my ajax call to my controller.
Mudasir.Khan
All-Star
15346 Points
3142 Posts
Re: calling controller method using ajax
Jun 25, 2012 12:34 PM|LINK
instead of url: '<%=Url.Action("Index", "Menu")%>',just try url:/ControllerName/ruddj16
Member
59 Points
92 Posts
Re: calling controller method using ajax
Jun 25, 2012 12:35 PM|LINK
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
function displayMenu() { window.alert("hello world"); $.ajax({ type: "POST", url: '@Url.Action("Temp")', 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: function (xhr, desc, error) { window.alert('description' + desc); window.alert('error' + error); } }); }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!
DannyMualim
Member
260 Points
77 Posts
Re: calling controller method using ajax
Jun 25, 2012 12:41 PM|LINK
Hi Ruddj16,
Are you using [HttpPost] for the Action like below query? If not, you should change the ajax type to "Get" [second query]
[HttpPost] public ActionResult Index() { ... }Javascript
Hopefully, this answers your question.
Danny Mualim
ruddj16
Member
59 Points
92 Posts
Re: calling controller method using ajax
Jun 25, 2012 12:44 PM|LINK
Heres the controller with the dummy method.
public JsonResult Temp() { string h = "Hello world"; return new JsonResult() { Data = null }; }Now when I run it, I get an internal server error value from my alerts.
DannyMualim
Member
260 Points
77 Posts
Re: calling controller method using ajax
Jun 25, 2012 12:51 PM|LINK
Hmmm... are you using the ajax query same as the one that you metion? or different one? if different, could you post it here?
Danny
ruddj16
Member
59 Points
92 Posts
Re: calling controller method using ajax
Jun 25, 2012 12:52 PM|LINK
here's the ajax call
function displayMenu() { window.alert("hello world"); $.ajax({ type: "POST", url: '@Url.Action("Temp", "Logon")', 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: function (xhr, desc, error) { window.alert('description' + desc); window.alert('error' + error); } }); }DannyMualim
Member
260 Points
77 Posts
Re: calling controller method using ajax
Jun 25, 2012 12:54 PM|LINK
Could you try below query? I changed the type from "POST" to "GET"
function displayMenu() { window.alert("hello world"); $.ajax({ type: "GET", url: '@Url.Action("Temp", "Logon")', 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: function (xhr, desc, error) { window.alert('description' + desc); window.alert('error' + error); } }); }Cheers,
Danny
ruddj16
Member
59 Points
92 Posts
Re: calling controller method using ajax
Jun 25, 2012 12:57 PM|LINK
thanks Danny,
Sadly it produces the same result.
Would more code be helpful?
DannyMualim
Member
260 Points
77 Posts
Re: calling controller method using ajax
Jun 25, 2012 01:13 PM|LINK
Ok, I just did experiment. please removed datatype: "JSON".
function displayMenu() { window.alert("hello world"); $.ajax({ type: "GET", url: '@Url.Action("Temp", "Logon")', //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: function (xhr, desc, error) { window.alert('description' + desc); window.alert('error' + error); } }); }Hopefully, this works at your PC.
Danny Mualim