通过jQuery GET调用传递参数在服务器端收到null

Oip*_*oks 2 .net javascript c# asp.net jquery

我正在尝试GET在jQuery中传递参数,这是我在做什么

function getChirurghi() {
    var id = "1";
    $.ajax({
        type: "GET",
        url: "/api/ControllerName/GetDataHere",
        contentType: "application/json; charset=utf-8",
        data: id,
        dataType: "json",
        success: function (data) {
            console.log(data);
        },
        failure: function (data) {
            alert(data.responseText);
        },
        error: function (data) {
            alert(data.responseText);
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

在服务器端调用了控制器,但是我得到的数据始终为空...

[HttpGet]
public IEnumerable<TypeOfObject> GetDataHere([FromBody]string id)
{}
Run Code Online (Sandbox Code Playgroud)

你知道那是怎么回事吗?

Ror*_*san 5

您需要为该值提供一个键,以便ModelBinder可以识别并使用它:

data: { id: id },
Run Code Online (Sandbox Code Playgroud)

您还需要[FromBody]在操作签名中删除该属性,因为GET数据是通过URL发送的(作为查询字符串的一部分,或作为路由结构)。

最后,的options对象$.ajax()没有failure属性,因此可以删除,因为它是多余的。