我有一个Web API服务调用,可以更新用户的首选项.不幸的是,当我从jQuery ajax调用调用这个POST方法时,请求参数对象的属性总是null(或默认值),而不是传入的内容.如果我使用REST客户机(我使用Postman)调用相同的精确方法,它的效果会非常好.我不知道我做错了什么,但我希望有人以前见过.这相当简单...

Here's my request object:

public class PreferenceRequest
{
    [Required]
    public int UserId;

    public bool usePopups;
    public bool useTheme;
    public int recentCount;
    public string[] detailsSections;
}

下面是我在UserController类中的控制器方法:

    public HttpResponseMessage Post([FromBody]PreferenceRequest request)
    {
        if (request.systemsUserId > 0)
        {
            TheRepository.UpdateUserPreferences(request.UserId, request.usePopups, request.useTheme,
                                         request.recentCount, request.detailsSections);

            return Request.CreateResponse(HttpStatusCode.OK, "Preferences Updated");                
        }
        else
        {
            return Request.CreateErrorResponse(HttpStatusCode.NotAcceptable, "You must provide User ID");
        }
    }

Here's my ajax call:

var request = {
    UserId: userId,
    usePopups: usePopups,
    useTheme: useTheme,
    recentCount: recentCount,
    detailsSections: details
};

$.ajax({
    type: "POST",
    data: request,
    url: "http://localhost:1111/service/User",
    success: function (data) {
        return callback(data);
    },
    error: function (error, statusText) {
        return callback(error);
    }
});

I've tried setting the dataType & contentType to several different things ('json', 'application/json', etc) but the properties of the request object are always defaulted or null. So, for example, if I pass in this object:

var request = {
  UserId: 58576,
  usePopups: false,
  useTheme: true,
  recentCount: 10,
  detailsSections: ['addresses', 'aliases', 'arrests', 'events', 'classifications', 'custody', 'identifiers', 'phone', 'remarks', 'watches']
}

I can see a fully populated request object with the valid values as listed above. But in the Web API controller, the request is there, but the properties are as follows:

  UserId: 0,
  usePopups: false,
  useTheme: false,
  recentCount: 0,
  detailsSections: null

FYI - I'm not doing ANY ASP.Net MVC or ASP.NET pages with this project, just using the Web API as a service and making all calls using jQuery $.ajax.

知道我做错了什么吗?谢谢

UPDATE: I just want to note that I have many methods in this same Web API project in other controllers that do this exact same thing, and I am calling the exact same way, and they work flawlessly! I have spent the morning comparing the various calls, and there doesn't appear to be any difference in the method or the headers, and yet it just doesn't work on this particular method.

我还try 切换到PUT方法,但得到了完全相同的结果-请求对象进入,但没有用正确的值填充.令人沮丧的是,我在这个项目中有大约20个控制器类,而POST在所有这些类中都可以工作……

推荐答案

这似乎是Asp的常见问题.Net WebAPI

public HttpResponseMessage Post(Object model)
        {
            var jsonString = model.ToString();
            PreferenceRequest result = JsonConvert.DeserializeObject<PreferenceRequest>(jsonString);
        }

Json相关问答推荐

Azure Devops Pipeline:SON字符串变量丢失所有双引号

如何让JSON子查询在没有行的情况下返回空数组而不是NULL

NoneType 对象的 Python 类型错误

Jolt 变换以展平 json 字符串数组

如何强制仅有一个元素的数组在JSON中生成方括号

在Databricks中如何将JSON文件作为字典读取

使用 jq 和脚本 bash 映射两个 json

每次在 SoapUI 中发送请求时,将 JSON 响应的子 node 分配给项目变量

如何使用nifi从json文件中过滤属性

Shell脚本空格转义

我如何将 JSON 格式与 flutter 一起使用?帮助使用 Gamebanana api

Powershell 7.2:ConvertFrom-Json - 日期处理

传统编程语言等价于动态 SQL

如何将从嵌套 Select 返回的空值转换为空数组?

JQuery,使用 GET 方法发送 JSON 对象

如何在 Go 中生成带有排序键的 JSON?

Angular 2/Web Api - json 解析错误语法错误意外结束输入

按 JSON 数据类型 postgres 排序

以 unicode 将 pandas DataFrame 写入 JSON

在自定义 JsonConverter 的 ReadJson 方法中处理空对象