简体   繁体   English

c#web api post request null

[英]c# web api post request null

Using c# web api server. 使用c#web api服务器。

my client call looks like: 我的客户电话看起来像:

$.ajax({
    type: method,
    url: urls.server + url,
    data: (method === "POST" ? '='+JSON.stringify(data) : data),
    dataType: "json",
    success: function(data){
        callback(data);
    },
    error: function(err) {
        onError(err);
    }
});

My C# server method looks like: 我的C#服务器方法如下:

[HttpPost, Route("All/GetMyTeam")]
public POST_GetMyTeam_Response Post(GetMyTeam_Request request)
{
    return new POST_GetMyTeam_Response();
}

Now, whenver i use the 'Advanced Rest Client' which is a Google's Chrome plugin I get my request perfectly fine. 现在,当我使用Google的Chrome插件“高级休息客户端”时,我的请求完全正常。 But, If I send this same request (I see in the network's area in chrome's debug window), the payloads are the same, the request arrives but all fields inside are null. 但是,如果我发送相同的请求(我在chrome的调试窗口的网络区域中看到),有​​效负载是相同的,请求到达但内部的所有字段都为空。

public class GetMyTeam_Request
{
    public string UserId;
    public string TeamId;
}

I also tried removing the '=' from the json client but I noticed most answer are leading to this addition (though they never told it suppose to be wrapped with apostrophe, but otherwise it's not working ). 我也尝试从json客户端删除'=' ,但我注意到大多数答案导致了这个添加(尽管他们从未告诉过它假设用撇号包裹,但是否则它不起作用)。 Tried to add contentType-application/json in both headers or as-is field in the ajax request.. 试图在ajax请求的头文件或as-is字段中添加contentType-application / json ..

TIA. TIA。

In your ajax request specify 在你的ajax请求中指定

contentType: 'application/json'

and also you don't need "=" string appended before json string data 而且你不需要在json字符串数据之前附加"="字符串

data: (method === "POST" ? '='+JSON.stringify(data) : data),
                         //^^^^ here

Remove the = sign and it should work. 删除=符号,它应该工作。

$.ajax({
    type: method,
    url: urls.server + url,
    data: (method === "POST" ? JSON.stringify(data) : data),
    dataType: "json",
    contentType: 'application/json',
    success: function(data){
        callback(data);
    },
    error: function(err) {
        onError(err);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM