0

I have this method on one of my controllers for getting a dropdownlist:

public JsonResult GetPeople(int roleId)
{
    return Json(_uow.GetPeople(roleId).Select(e => new SelectListItem { Text = e.Name, Value = e.Id.ToString() }).ToList());
}

so basically it gets a whole bunch of people within a particular role and returns a selectlistitem list in json.

I'm unsure whether it should be decorated with [HttpGet] or [HttpPost]. I'm thinking it should be a get because that's essentially what your doing, getting info from the server even though you do post an id.

So it has no decoration so by default is a GET.

This is how I call it in jquery:

$.get('/People/GetPeople', { roleId: $('#roleddl').val() }, function (data) {

    $(data).each(function () {
        $("<option value=" + this.Value + ">" + this.Text + "</option>").appendTo(peopleddl);
    });

});

So here I'm using the $.get. It didn't work. So I was thinking it's returning json so I tried $.getJSON which didn't work. So then I tried $.post and it worked as:

$.post('/People/GetPeople', { roleId: $('#roleddl').val() }, function (data) {

    $(data).each(function () {
        $("<option value=" + this.Value + ">" + this.Text + "</option>").appendTo(peopleddl);
    });

});

So I'm wondering why is a jquery post call working when the method it is calling is no a POST.

Also even though this works what combination should I have. ie. What should the method be decorated with and what should I use to call it?

4

1 に答える 1

0

Json を返すことができるように、return ステートメントを変更する必要があります。

return Json(data, JsonRequestBehavior.AllowGet);

Json には、GET 応答で返される既知の脆弱性があります (詳細はこちら: http://haacked.com/archive/2009/06/25/json-hijacking.aspx ) 。

于 2012-06-28T04:23:15.157 に答える