We’re using jQuery.ajax() methods to request server data on a number of pages on our MVC 3 web site. These requests are always marked with the ‘POST’ ajax parameter type and are typically invoked on page load or perhaps on a timer, that is to say, they are not a result of a user action (e.g. a mouse-click).
When we look at the (Elmah) error log we see a number of entries as follows:
A public action method ‘GetMessageStats’ was not found on controller ‘Inbox.WebUI.Areas.Application.Controllers.StatusController’.
The controller action is marked with the [HttpPost] e.g.
[HttpPost]
public JsonResult GetMessageStats()
{
MessageStatsViewModel model = new MessageStatsViewModel
{
TotalNoMessages = MailDB.GetMessageCount(),
MessagesInQueue = MailDB.GetQueueLength()
};
return Json(model);
}
and here is the invoking client script:
$(function() {
var $totalMessages = $("#total-messages"),
$queuedMessages = $("#queued-messages");
function getStats() {
$.ajax({
type: "POST",
url: "/Application/Status/GetMessageStats",
dataType: "json",
cache: false,
success: function (data) {
$totalMessages.text(data.TotalNoMessages);
$queuedMessages.text(data.MessagesInQueue);
setTimeout(function() {
getStats();
}, 15000);
},
error: function (xmlHttpRequest, errorMessage, exception) {
throw errorMessage;
}
});
}
getStats();
});
On investigation it appears that some browsers (IE7/IE8, but maybe others) seem to issue a GET request in addition to the required POST request. It is noted that the user interface responds and behaves correctly under these browsers so the POST request is being serviced.
Elmah is reporting the user agent as:
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
Has anyone else seen this problem? If so, have you found a way to avoid it?
Thanks.
For what it is worth, re: the superfluous GET, I found that there were some toolbars/security services that would ‘check’ urls to make sure they were valid. I believe it was TrendMicro who had a Web of Trust type plugin that would re-poll sites to examine them for malware.
Have you tried using the shorthand ajax methods, like $.post()? Also, if you’re getting data, is there a specific reason you need a POST?
If you generate a request through Fiddler or your tool of choice, do you get the same error in the response?