簡體   English   中英

為什么我的Ajax帖子在進入后端時有內容,但是我的模型和控制器卻收到null

[英]Why does my Ajax post have content when going to backend but my model and controller receive null

AJAX Post在調試器中顯示正確的內容,但是我的控制器和模型接收到空值。

我已經以多種方式更改了我的Ajax帖子。 我也剛發送了一個字符串,並將一個字符串作為我的控制器參數。 這可行,但是我希望發送整個對象。

        var BuildContent = [{ 'Cal': Cal, 'BarLength': BarLength, 'Color': 
        Color }];
        content = JSON.stringify(BuildContent);
        $.ajax({
            type: "POST",
            url: "../Build/Build",
            data: {'content':content},
            ContentType: "application/json",
            dataType: "json",
        });
    public class BuildController : Controller
    {


        [HttpPost]
        public BuildContent Build (BuildContent content) 
        {
            //BuildService.GetURG(data);
            return content;
        }
    }
    public class BuildContent
    {

        public string Cal { get; set; }
        public string BarLength { get; set; }
        public string Color { get; set; }
    }

因此,當POST到達控制器時,我會看到[Cal = null,BarLength = null,Color = null]在Chrome網絡調試器中,我可以正確看到內容。 例如內容:[{“ Cal”:“ 5.56”,“ BarLength”:“ 16 \\”“,” Color“:” Black“}]

您不是在發送對象數組,而是僅發送一個對象。 因此,無需將對象包含在數組初始化器中。

在下面,我創建了一個JavaScript對象,然后將其轉換為JSON。 試試這個,讓我知道如何進行。

var buildContent = {'Cal': 'Testing123...', 'BarLength': 'Noot Noot', 'Color': 'Red'};
var jsonData = JSON.stringify(buildContent);
$.ajax({
    type: "POST",
    url: "../Build/Build",
    data: jsonData,
    dataType: 'json',
    contentType: 'application/json;'
});

編輯:或者嘗試完整地發送對象,而不將其序列化為JSON。

var buildContent = {'Cal': 'Testing123...', 'BarLength': 'Noot Noot', 'Color': 'Red'};
$.ajax({
    type: "POST",
    url: "../Build/Build",
    data: buildContent
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM