0

I have a method which Is accesed using Get

[HttpGet]
[AdminAuthorization]
public ActionResult MakeReservation(ReservationModel m)
{

    return PartialView(m);
}

here Ajax Code:

$.ajax({
        url: "/DeviceUsage/MakeReservation",
        type: "GET",
        contentType: "application/json; charset=utf-8",
        dataType: 'json',
        data: JSON.stringify({ data: Ids }),
        error: function (data) {
            alert("Dodanie  nie powiodło się  Jeden lub wiecej numerów seryjnych nie są unikalne " + data);
        },
        success: function (data) {
            $('#ProperModal.modal-body').html(data);
            $("#Modal").modal('show');
            //if (data === "sukces") {

        }
    });

If I change method description and ajax type to POST function works. How should I modify this code to make it work wiht GET calls?

alexmac
  • 19,087
  • 7
  • 58
  • 69
szpic
  • 4,346
  • 15
  • 54
  • 85

3 Answers3

2

You need to use JsonRequestBehavior.AllowGet in your controller. For more information you could read this answer on SO

And I think it is good practise to return Json (not PartialView) in your action (for ajax). If you want to return PartialView, you could use this technique

Community
  • 1
  • 1
Boris Parfenenkov
  • 3,219
  • 1
  • 25
  • 30
  • Thanks. But as I read the `AllowGet` is placed in return. But as you can see my method doesn't return `JSON` – szpic Mar 27 '14 at 08:45
  • Oh, okey. Let's see about Json vs PartialView http://stackoverflow.com/questions/1471066/partial-views-vs-json-or-both – Boris Parfenenkov Mar 27 '14 at 08:48
1

You don't need to explictly tell the HttpGet, By Default, it takes it as HttpGet, but if you put HttpPost attribute, then it does not work on Get Requests.

Same is the case for Jquery ajax, if you don't tell it, its get or post request, it by default makes a get request to server

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
0

Remove contentType and dataType: 'json' (this indicates you are returning json, but your code returns a partial view). And Remove JSON.stringify as jQuery accept your JS object directly. Haven't tested it, but it should work.

Hannes
  • 426
  • 3
  • 12