Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在jQuery AJAX/Web API中使用POST-参数始终为null_Jquery_Ajax_Asp.net Web Api - Fatal编程技术网

无法在jQuery AJAX/Web API中使用POST-参数始终为null

无法在jQuery AJAX/Web API中使用POST-参数始终为null,jquery,ajax,asp.net-web-api,Jquery,Ajax,Asp.net Web Api,我似乎无法使绑定适用于复杂类型。我已经尝试了这里的大多数建议,但似乎无法使其发挥作用。参数始终为空 function createFile(filename, x, y, width, height) { var image = { Filename: filename, X: x, Y: y, Width: width, Height: height }; $.ajax({

我似乎无法使绑定适用于复杂类型。我已经尝试了这里的大多数建议,但似乎无法使其发挥作用。参数始终为空

function createFile(filename, x, y, width, height) {
    var image = {
        Filename: filename,
        X: x,
        Y: y,
        Width: width,
        Height: height
    };

    $.ajax({
        type: "POST",
        url: Server.Domain() + "media/createFile",
        data: JSON.stringify(image),
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function (result) {
            alert('success');
        },
        error: function (xhr, err) {
            //
        }
    });
}
以及Web API方法:

    [Route("api/media/createFile")]
    [HttpPost]
    public bool CreateFile(ItemDto item)
    {
        bool success = false;
        try
        {
            //
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return success;
    }
和类型:

public class ItemDto
{
    public string Filename { get; set; }
    public int X { get; set; }
    public int Y { get; set; }
    public int Width { get; set; }
    public int Height { get; set; }
}
Web API被调用,但'item'参数始终为null。我尝试添加FromBody,删除内容类型,有人说它不应该出现,但我认为不应该出现这种情况,因为我正在传递JSON,但仍然没有效果

编辑:

这就是发送的内容 {文件名:30.jpg,X:29.9999999999999 3,Y:29.9999999999 3,宽度:240.00000000000003,高度:240.00000000000003}

更新:

将JavaScript对象更改为此绑定成功:

var image = {
        Filename: "a",
        X: "1",
        Y: "2",
        Width: "3",
        Height: "4"
    };
发送的值为

"{"Filename":"a","X":"1","Y":"2","Width":"3","Height":"4"}"
我的原始值有问题吗

更新2:


我将X改为1.1111111,并使用双引号和不使用双引号进行测试。该参数不为null,但在Web API中X等于零。

事实证明,数据类型是错误的:

public class ItemDto
{
    public string Filename { get; set; }
    public int X { get; set; }
    public int Y { get; set; }
    public int Width { get; set; }
    public int Height { get; set; }
}

我发送的是带小数点的数字。将数据类型更改为decimal解决了此问题。

您是否查看了控制台中发送的内容?是的,很抱歉,忘记包含。请参阅edit,您似乎需要执行X:Math.roundxBetter选择是发送整数而不是浮点