ASP.NET MVC 4: enabling XmlHttpRequest message for controller with $ .ajax ()

Seems simple, but I've been looking around the world all day with a bit of regression! Please help. I have a simple post request to an MVC controller ...

 $(document).ready(function () {
    $('#json_request').on("click", function () {
        // send request to server for menu json data
        $.ajax({
            type: "POST", 
            url: location.protocol + "//" + location.host + "/Tablet/Menu",
            data: { restaurantId: $('#restaurantId option:selected').val(), mealSessionId: $('#mealSessionId option:selected').val() },
            success: function (menuData) {alert(menuData); },
            error: function () { alert("failed"); }
        });
    });
});

      

The request just won't reach the controller! The app works fine when I submit the request as an Html form. It works great with Visual Studio development server. I am getting 404 error with IIS 7.0 / ASP.NET / MVC 4. Maybe contentType: application / x-www-form-urlencoded is not getting through http protocol filters. Do I have to install them specifically? How? Thank you for your help. I am not sending the request as json, so I have not tried contentType: application / json.

Controller / Action:

[HttpPost]
    public ActionResult Menu(short restaurantId, short mealSessionId)
    {
        try
        {
            MenuInput menuInput = new MenuInput(restaurantId, mealSessionId);
            menuInput.LoadMenu();
            if (Request.IsAjaxRequest())
            {
                MemoryStream ms = new MemoryStream();
                menuInput.AsJson(ms);
                string jsonString = Encoding.UTF8.GetString(ms.ToArray());
                JsonResult result = Json(jsonString, "text/x-json");
                ms.Close();
                return result;
            }
            else
            {
                return View("MenuInformation", menuInput);
            }
        }
        catch (Exception ex)
        {
            System.Console.Write(ex.ToString());
            return View();
        }
    }

      

+3
ajax asp.net xmlhttprequest


source to share


No one has answered this question yet

See similar questions:

17
How to send Json object (or string data) from Javascript xmlhttprequest to MVC controller

or similar:

635
JQuery AJAX POST example with PHP
611
How do you handle multiple submit buttons in ASP.NET MVC Framework?
559
ASP.NET MVC - setting custom IIdentity or IPrincipal
537
How do I create a dropdown from an enum in ASP.NET MVC?
527
XmlHttpRequest error: Origin null is not allowed Access-Control-Allow-Origin
481
Compile views in ASP.NET MVC
479
Send POST data using XMLHttpRequest
443
Can ASP.NET MVC Controller return an image?
333
How many concurrent AJAX (XmlHttpRequest) requests are allowed in popular browsers?
3
How does ASP.NET know if a request is AJAX or not?



All Articles
Loading...
X
Show
Funny
Dev
Pics