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();
        }
    }
4
  • 1
    Can you post your controller action method? Commented Jan 31, 2013 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. Commented Jan 31, 2013 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. Commented Jan 31, 2013 at 13:02
  • Use XMLHttpRequest instead of AJAX, see stackoverflow.com/questions/6055714/…. Commented Feb 18, 2017 at 22:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.