将Json对象参数发送到.netCore中的Web Api控制器

将Json对象参数发送到.netCore中的Web Api控制器,json,ajax,string,asp.net-web-api,asp.net-core,Json,Ajax,String,Asp.net Web Api,Asp.net Core,这是GoogleAPI-Javascript代码 var output = new Object(); output.PlaceID = place.place_id; output.Longitude = place.geometry.location.lng(); output.Latitude = place.geometry.location.lat(); $.ajax({ headers: { 'Accept': 'application/json',

这是GoogleAPI-Javascript代码

var output = new Object();
output.PlaceID = place.place_id;
output.Longitude = place.geometry.location.lng();
output.Latitude = place.geometry.location.lat();

$.ajax({
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    },
    url: 'api/StorePlaces/Get',
    type: 'POST',
    data: { "value":output },
    success: function (result) {
        // Do something with the result
    }
});
如何在控制器上接收数据

    // GET api/values/5
    [HttpPost("{PlaceDetails}")]
    public string Get(PlaceDetails value)
    {
        return "value";
    }
在这种情况下,我得到空值

我无法发送字符串,如果我可以这样发送对象,那就更好了

这可以用作接收对象

public class PlaceDetails
{
    public string PlaceID { get; set; }
    public string Longitude { get; set; }
    public string Latitude { get; set; }
}

您的代码有多处错误,可以先参考一些初学者教程吗

首先,您必须查看您发送的对象,这是非常明显的

你正在发送

{
    "value" : {
        "PlaceID" : "",
        "Longitude " : "",
        "Latitude " : ""
    }
}
预期答案在哪里

{
    "PlaceID" : "",
    "Longitude " : "",
    "Latitude " : ""
}
因此,您必须在JavaScript中使用:

$.ajax({
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    },
    url: 'api/StorePlaces/Get',
    type: 'POST',
    // do not wrap it in object with value property here!!!!
    data: JSON.stringify(output),
    success: function (result) {
        // Do something with the result
    }
});
第二,你的控制器动作(当它是post请求时,为什么叫它
Get
?)。。。
[HttPost(“{PlaceDetails}”)]
属性显然是错误的

这将要求路由中有一个
PlaceDetails
参数。你不喜欢这样的人!把它拿走。此外,缺少
[FromBody]
属性来告诉它从http请求主体反序列化模型

[HttpPost]
public string Get([FromBody]PlaceDetails value)
{
    return "value";
}

将ajax中的请求类型更改为“POST”,并将您的方法更改为接受POST,并将参数类型更改为“PlaceDetails”@Dalton I get null value,修改数据的内容:put
data:output
并删除
[FromBody]
@ARUNEdathadan:另外,请不要用你从第一个问题开始就尝试过的东西来编辑这个问题,因为它可能会完全改变问题的含义。是的,回答得不错,但是关于初学者的夸张说法对他有帮助吗?让别人看到不加掩饰的侮辱会有帮助吗?这真的让你感觉好点了吗?