MVC 3 JSON call not working in IIS MVC 3 JSON call not working in IIS ajax ajax

MVC 3 JSON call not working in IIS


Never hardcode urls like this because when you deploy your application there could be a virtual directory prepended to your urls:

url: '/SearchAjax/SearchAccount',

Always use Url helpers when dealing with urls:

url: '<%= Url.Action("SearchAccount", "SearchAjax") %>',

So here's how I would refactor your code:

function PerformLookup() {    var _accountNumber = $('#accountNumber').val();    $.ajax({        url: '<%= Url.Action("SearchAccount", "SearchAjax") %>',        type: 'POST',        data: JSON.stringify({ _accountNumber: _accountNumber }),        dataType: 'json',        contentType: 'application/json; charset=utf-8',        success: function (data) {            UpdateTable(data);        },        error: function () {            alert('An error occurred while performing the search.');        }    });    return false;}

or if this PerformLookup function is called when some link is clicked, I would have the link generated with an HTML helper:

<%= Html.ActionLink(    "Perform search",     "SearchAccount",     "SearchAjax",     null,     new { id = "search" }) %>

and then simply AJAXify it:

$(function() {    $('#search').click(function() {        var _accountNumber = $('#accountNumber').val();        $.ajax({            url: this.href,            type: 'POST',            // Probably no need to send JSON request            // so I've replaced it with a standard            // application/x-www-form-urlencoded POST request            data: { _accountNumber: _accountNumber },            dataType: 'json',            success: function (data) {                UpdateTable(data);            },            error: function () {                alert('An error occurred while performing the search.');            }        });        return false;    });});

And finally I would strongly recommend you using FireBug which is an excellent tool allowing you to debug this kind of problems as it shows all AJAX requests and what's happening between the client and the server.


just put the full URL in javascript:

$.ajax({    type: "POST",    url: "/Appnamehere/Controller/Action", ...

this work fine..