Ajax 函数总是返回 undefined

问题描述 投票:0回答:1

我需要将数据发布到 action Controller model ,数据进入模型,但最终返回“undefined”。 我的代码有什么问题?

    $(function () {
    $("#submit").click(function () {
        var jdata = {
            CompanyCode: 1,
            CompanyName: "test",
        };
        $.ajax({
            type: "POST",
            url: "/Services/getdata",
            data: JSON.stringify(jdata),
            contentType: "application/json; charset=utf-8",
            dataType: "json",             
            success: function (response) {
                alert("Hello: " + response.Name + " .\nCurrent Date and Time: " + response.DateTime);
            },
            failure: function (response) {
                alert(response.responseText);
            },
            error: function (response) {
                alert(response.responseText);
            }
        });
    });
});

C#

    [HttpPost]
    public async Task<IActionResult> getdata([FromBody] model tmpModel)
    {
        return View();
    }
ajax asp.net-core asp.net-mvc-4
1个回答
0
投票

当您在 AJAX 调用中指定

dataType: 'json'
时,这意味着您期望从服务器返回
json
数据类型。

return View();
行将
getdata.cshtml
视图呈现为 HTML 文档,并将其作为响应发送回服务器。它不包括
Name
DateTime
属性。

这就是为什么它们是“未定义”

© www.soinside.com 2019 - 2024. All rights reserved.