3

Seems a simple thing but I've spent all day looking through posts all over the web with little recourse! Please help. I have a simple request to be posted to a 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 post the request as a Html form. It works fine with the Visual Studio Development Server too. I get 404 error with IIS 7.0 / ASP.NET / MVC 4. Possibly, contentType: application/x-www-form-urlencoded does not get through the http-protocol filters. Do I have to set those specifically? How? Thanks for your help. I am not send the request as a json, so i did not try 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();
        }
    }
  • 1
    Can you post your controller action method? – Mathew Thompson Jan 31 '13 at 12:06
  • Sure. Posted below. I do not think the problem is with the controller. I get my Json with the VS development server. When i run from the website in debug mode, I can see that the request does not get to the controller. – user2028859 Jan 31 '13 at 12:40
  • Do you get the error alert? If not, try changing your parameters to an `int` just to see if that makes any difference. – Mathew Thompson Jan 31 '13 at 13:02
  • Use XMLHttpRequest instead of AJAX, see http://stackoverflow.com/questions/6055714/how-to-send-json-object-or-string-data-from-javascript-xmlhttprequest-to-mvc-c. – Tomas Kubes Feb 18 '17 at 22:56

0 Answers0