gpt4 book ai didi

c# - 在 C# 中使用 Ajax(url) 传递多个参数

转载 作者:行者123 更新时间:2023-12-01 03:40:09 24 4
gpt4 key购买 nike

在 MVC 中使用 Ajax 获取多个参数时遇到问题。我有两个需要输入的字段。用户名和评论文本的输入字段。

我在 ajax 的 url 部分定义这些参数。当我只传递一个参数时它工作正常(单独尝试时对两个参数都有效),但是一旦我尝试两个参数,后一个参数就不起作用。

Ajax函数:

$(function () {
$("#button").click(function () {
$.ajax({
type: "GET",
contentType: "application/json; charset=utf8",
url: "Home/AddComment?CommentText=" + $("#CommentText").val() + "&Username=" + $("Username").val(),
dataType: "json",
success: function (Comment) {
//some function
},
error: function (xhr, err) {
//some code
}
});
});
});

有什么想法吗?我应该通过“数据”传递参数吗?

编辑:*这是应该捕获这些参数的 Controller 。*

 public JsonResult AddComment(string commentText, string username)
{
Comment c = new Comment() { CommentText = commentText, Username = username };
CommentRepository.Instance.AddComment(c);
return Json(GetComments(), JsonRequestBehavior.AllowGet);

}

最佳答案

你可以使用这样的东西:

Ajax

$.ajax({
type: 'GET',
url: 'Home/AddComment',
data: { CommentText: $("#CommentText").val(),
Username: $("#Username").val() },
cache: false,
success: function (result) {
desc = result;
}
});

然后在您的 Controller 中:

public string AddComment(string CommentText, string Username)
{
//your code here
}

希望这对您有帮助。

关于c# - 在 C# 中使用 Ajax(url) 传递多个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22685552/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com