jQuery $.ajax 将 null 值传递给 MVC 控制器

发布于 2024-12-05 06:19:31 字数 1907 浏览 0 评论 0原文

我已经为这个问题苦苦挣扎了一段时间了。我已遵循类似问题中提供的所有建议,但没有成功。

正如标题所示,我的控制器未接收到我的 $.ajax 传递的数据。

jQuery:

var data = {
    id: id,
    app: app
};


    $.ajax({
        type: "POST",
        url: "/Utility/FetchTransactionLog",
        data: JSON.stringify(data),
        contentType: "application/json;",
        success: function (data) {
            if (data) {
                h.resultDiv.html(data);
            }
            else {
                h.resultDiv.html("");
                alert("No Log Found");
            }
        }
    });

控制器:

//id and app receives null values
public ActionResult FetchTransactionLog(string id,string app) {
    UtilityModels util = new UtilityModels(app);
    List<ResultModel> result = util.FetchTransactionLog(id);
    return View("LogResult", result);            
}

Global.asax 中的路由:

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Utility", action = "Index", id=UrlParameter.Optional } // Parameter defaults
);

routes.MapRoute(
     "FetchLog",
     "Utility/FetchTransactionLog/{id}/{app}",
     new { controller = "Utility", action = "FetchTransactionLog", id = "", app = "" } // Parameter defaults
 );      

我做错了什么?

更新

以下内容按照 Samich 的建议进行:

$.ajax({
            type: "POST",
            url: "/Utility/FetchTransactionLog/" + id + "/" + app,
            data: "{}",
            contentType: "application/json;",
            success: function (data) {
                if (data) {
                    h.resultDiv.html(data);
                }
                else {
                    h.resultDiv.html("");
                    alert("No Log Found");
                }
            }
        });

i've been struggling with this problem for quite some time. I've followed all suggestions provided in similar problems but without success.

As the title says, data passed by my $.ajax is not received by my controller.

jQuery:

var data = {
    id: id,
    app: app
};


    $.ajax({
        type: "POST",
        url: "/Utility/FetchTransactionLog",
        data: JSON.stringify(data),
        contentType: "application/json;",
        success: function (data) {
            if (data) {
                h.resultDiv.html(data);
            }
            else {
                h.resultDiv.html("");
                alert("No Log Found");
            }
        }
    });

Controller:

//id and app receives null values
public ActionResult FetchTransactionLog(string id,string app) {
    UtilityModels util = new UtilityModels(app);
    List<ResultModel> result = util.FetchTransactionLog(id);
    return View("LogResult", result);            
}

Route in Global.asax:

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Utility", action = "Index", id=UrlParameter.Optional } // Parameter defaults
);

routes.MapRoute(
     "FetchLog",
     "Utility/FetchTransactionLog/{id}/{app}",
     new { controller = "Utility", action = "FetchTransactionLog", id = "", app = "" } // Parameter defaults
 );      

What am i doing wrong?

Update

The following works as suggested by Samich:

$.ajax({
            type: "POST",
            url: "/Utility/FetchTransactionLog/" + id + "/" + app,
            data: "{}",
            contentType: "application/json;",
            success: function (data) {
                if (data) {
                    h.resultDiv.html(data);
                }
                else {
                    h.resultDiv.html("");
                    alert("No Log Found");
                }
            }
        });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

与他有关 2024-12-12 06:19:31

尝试将您的 id 和应用程序附加到您在路由 /Utility/FetchTransactionLog/{id}/{app} 中指定的网址,

可能是因为您的路由需要查询字符串中的值,但找不到它在那里,所以路由注册提供的默认值

try just to append your id and app to your url as you specified in the routes /Utility/FetchTransactionLog/{id}/{app}

Probably because your routing expects values in query string and doesn't find it there, so the default values provided from the routing registration

嘿嘿嘿 2024-12-12 06:19:31

为什么要对数据进行 JSON.stringify?您应该按原样传递对象,就像

data: data,

文档 说的那样,如果您通过在对象中,它会自动转换为字符串。通过传入包含 JSON 的字符串,您会混淆 jQuery,并且最终会将垃圾数据传递给 ASP(使用浏览器的开发工具来检查请求会立即产生这很明显)。

Why do you do JSON.stringify your data? You should just pass the object as it is, like

data: data,

The documentation says flat out that if you pass in an object, it will be converted to a string automatically. By passing in a string that contains JSON, you are confusing jQuery and it ends up passing garbage data to ASP (using your browser's developer tools to inspect the request would have immediately made this apparent).

梦中楼上月下 2024-12-12 06:19:31

您的 data 不应该是 JSON 字符串,只需将其保留为 Javascript 对象即可。 jQuery 会将其转换为 POST 参数,然后您的控制器可以读取这些参数。

$.ajax({
    type: "POST",
    url: "/Utility/FetchTransactionLog",
    data: data,
    success: function (data) {
        if (data) {
            h.resultDiv.html(data);
        }
        else {
            h.resultDiv.html("");
            alert("No Log Found");
        }
    }
});

Your data shouldn't be a JSON string, just leave it as a Javascript object. jQuery will convert this to POST parameters, which your controller can then read.

$.ajax({
    type: "POST",
    url: "/Utility/FetchTransactionLog",
    data: data,
    success: function (data) {
        if (data) {
            h.resultDiv.html(data);
        }
        else {
            h.resultDiv.html("");
            alert("No Log Found");
        }
    }
});
折戟 2024-12-12 06:19:31

我目前无法对此进行测试,但尝试将 - 更改

data: JSON.stringify(data), 

data: data,

JSON.stringify 将以阻止 jQuery 正确发布数据的方式格式化您的数据。如果您传递一个普通对象,一切都应该按预期工作。

I can't test this at the moment but try changing -

data: JSON.stringify(data), 

to

data: data,

JSON.stringify will format you data in a way that prevents jQuery posting it correctly. If you pass a normal object everything should work as expected.

臻嫒无言 2024-12-12 06:19:31

试试这个:

 $.ajax({
    type: "POST",
    url: "/Utility/FetchTransactionLog",
    data: { id: id, app: app },
    contentType: "application/json;",
    success: function (data) {
        if (data) {
            h.resultDiv.html(data);
        }
        else {
            h.resultDiv.html("");
            alert("No Log Found");
        }
    }
});

Try this :

 $.ajax({
    type: "POST",
    url: "/Utility/FetchTransactionLog",
    data: { id: id, app: app },
    contentType: "application/json;",
    success: function (data) {
        if (data) {
            h.resultDiv.html(data);
        }
        else {
            h.resultDiv.html("");
            alert("No Log Found");
        }
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文