所以我使用TEXTAREA
标签发送评论文字(最多1500个字符),但它不起作用。
当我使用几个单词发送时,它工作正常。
任何线索,伙计们?
错误
Failed to load resource: the server responded with a status of 400 (Bad Request)
JS
var commentText = $("#commentTextArea").val();
if (commentText.length > 0) {
var urlGetComments = window.location.protocol + '//' + window.location.host + "/Gadget/PostComment/" + userId + "/" + commentText;
$.ajax({
type: "POST",
url: urlGetComments,
data: "",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (dataComments) {
if (dataComments.html != null) {
$('#divCommentsData').html(dataComments.html);
}
}
});
}
C#
[AcceptVerbs("POST")]
[HttpPost]
[ValidateInput(false)]
public JsonResult PostComment(string userId, string commentText)
{
try
{
答案 0 :(得分:4)
您正在使用URL传递信息,许多服务器对URL中的字符有限制。由于您的操作方法接受POST请求,因此您可以将数据包含在“data”参数中,而不是将其附加到URL。
$.ajax({
type: "POST",
url: urlGetComments,
data: { 'commentText': commentText },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (dataComments) {
if (dataComments.html != null) {
$('#divCommentsData').html(dataComments.html);
}
}
});
答案 1 :(得分:2)
您可以将data
设置为:
data: "{userId:1, commandText:'cmd text'}",
并将url
更改为:
var urlGetComments = window.location.protocol + '//' + window.location.host + "/Gadget/PostComment;
答案 2 :(得分:2)
(有问题的评论)
首先,创建URL的最佳方法是使用URL.Action方法隐藏标记输入,如下所示:
<input type="hidden" id="url-gadget-comment"
value="@Url.Action("PostComment", "Gadget")" />
所以你可以把脚本放在html之外。
然后使用此值并通过“POST”发送数据:
var commentText = $("#commentTextArea").val();
if (commentText.length > 0) {
$.ajax({
type: "POST",
url: $('#url-gadget-comment').val(),
data: { userId: userId, commentText: commentText },
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (dataComments) {
if (dataComments.html != null) {
$('#divCommentsData').html(dataComments.html);
}
}
});
}
答案 3 :(得分:2)
在打电话之前先把它放好
jQuery.ajaxSettings.traditional = true;
答案 4 :(得分:1)
浏览器和服务器限制了查询字符串的长度,因此出现了HTTP 400错误。在ASP.Net中,默认情况下为2048,在web.config的HttpRuntime部分设置,请参阅http://msdn.microsoft.com/en-us/library/system.web.configuration.httpruntimesection.maxquerystringlength.aspx
您正在以错误的方式使用POST,并在网址中传递数据。您应该将数据放在POST请求的数据字段中。
您可以尝试:
var commentText = $("#commentTextArea").val();
if (commentText.length > 0) {
var urlGetComments = window.location.protocol + '//' + window.location.host + "/Gadget/PostComment/";
$.ajax({
type: "POST",
url: urlGetComments,
data: {"userId": userId, "commentText": commentText},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (dataComments) {
if (dataComments.html != null) {
$('#divCommentsData').html(dataComments.html);
}
}
});
}
答案 5 :(得分:1)
我最近遇到了同样的错误。 (帖子400错误)
我发现错误是因为我的$ .Ajax方法的数据部分需要更多引号。这对我有用。在CSHTML页面中:
var vals = cells.toString();
$.ajax({
type: "POST",
url: '@Url.Action("TheAction", "theControl")',
contentType: "application/json; charset=utf-8",
data: "{'IDS':'" + vals + "'}",
dataType: "json",
success: function (response) {
var input = "";
document.getElementById("SomeItem").innerHTML = input
},
failure: function (response) {
}
});
在控制中:
[HttpPost]
public JsonResult TheAction(string IDS)
{
ASPNETUSER userProfile = db.ASPNETUSERS.Find(User.Identity.GetUserId());
//Do something super cool with IDS
var retval = <do something with ids>;
return Json(retval , JsonRequestBehavior.AllowGet);
}
对我来说,关键是ajax这一部分中的所有引号,以便值vals用单引号括起来,而IDS用单引号括起来:
data: "{ 'IDS' : ' " + vals + " '} ",
对我来说,vals是一串逗号分隔的IDS,如果用户选择了页面上的所有IDS,则GET函数可能会太长。
答案 6 :(得分:0)
感谢大家!但对我来说最好的最终解决方案就是那个。
var commentText = $("#commentTextArea").val();
if (commentText.length > 0) {
var urlGetComments = window.location.protocol + '//' + window.location.host + "/Gadget/PostComment/`
var params = { userId = 1, commentText: commentText };
$.ajax({
type: "POST",
url: urlGetComments,
data: JSON.stringify(params),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (dataComments) {
if (dataComments.html != null) {
$('#divCommentsData').html(dataComments.html);
}
}
});
}
它基于Using jQuery AJAX to POST multiple variables to an ASP .NET MVC Controller