尝试使用Ajax进行简单传递。值artID和v给出正确的值。每次都会收到Ajax错误。
控制器=文章
方法= SaveRating
Ajax代码:
$.ajax({
type: "POST",
url: 'Url.Action("SaveRating","Article")',
data: {
articleID: artID,
rate: v
},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function () {
alert("success");
},
error: function () {
alert("fail");
}
});
});
控制器:
[HttpPost]
public static int SaveRating(int articleID, int rate)
{
....
}
答案 0 :(得分:1)
这样做。
public class Rating
{
public int ArticleID { get; set; }
public int Rate { get; set; }
}
[HttpPost]
public static int SaveRating(Rating rate)
{
//
}
客户端帖子
<script>
$.ajax({
type: "POST",
url: "/Article/SaveRating",
data: JSON.stringify({ rate:
ArticleID: "1",
Rate: "4"
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function() {
alert("success");
},
error: function() {
alert("fail");
}
});
</script>
答案 1 :(得分:0)
尝试按照ajax调用,
$.ajax({
type: "POST",
url: "/Article/SaveRating",
data: { articleID: '1', rate: '5' },
dataType: "json",
success: function(response) { alert(response); },
error: function(xhr, ajaxOptions, thrownError) { alert(xhr.responseText); }
});
答案 2 :(得分:0)
首先通过JSON.stringify将数据转换为json字符串,然后发送该数据。
var data = {
articleID: 1,
rate: 5
};
data = JSON.stringify(data);
$.ajax({
type: "POST",
url: '/Article/SaveRating',
data:data,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function () {
alert("success");
},
error: function () {
alert("fail");
}
});
答案 3 :(得分:0)
您的网址不正确,请将其更改为'@Url.Action("SaveRating","Article")'
:
$.ajax({
type: "POST",
url: '@Url.Action("SaveRating","Article")',
data: JSON.stringify({
articleID: artID,
rate: v
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function () {
alert("success");
},
error: function () {
alert("fail");
}
});