我是Mvc jason的新手我有跟随ajax功能,该功能工作正常并将数据发送到服务器操作,服务器也返回成功或失败但问题是JSON数据显示在新页面中
{"成功":真}
这是我的ajax功能
$('#save').click(function () {
var CourseID = $('.form-control span1').val();
var Type = $('.form-control span2').val();
var id =@Model.UserId
$.ajax({
url: '@Url.Action("AdminUser/Edit")',
type: 'POST',
data: { id: '@Model.UserId', CourseID: CourseID, Type: Type },
Success: function (response) {
if (response.Success) {
alert("done");
}
else {
alert("Errror");
}
}
});
});
这是我的控制器
[HttpPost]
public ActionResult Edit(int id, string CourseID, string type)
{
KBS_Virtual_TrainingEntities db = new KBS_Virtual_TrainingEntities();
var sc = (from course in db.CourseSubscriptions.ToList()
where course.Cours.ID.ToString() == CourseID && course.SubscriptionsType.ID.ToString() == type
select course).SingleOrDefault();
UserSubscription new_sub = new UserSubscription();
CourseSubscription sub_course = (CourseSubscription)sc;
if (sub_course != null)
{
new_sub.CourseSubscription = sub_course;
new_sub.CourseSubscriptionID = sub_course.ID;
new_sub.StartedDate = DateTime.Now;
new_sub.UserID = id;
db.UserSubscriptions.Add(new_sub);
db.SaveChanges();
return Json(new { Success = true });
}
return Json(new{Success = false});
}
答案 0 :(得分:1)
尝试将return Json(new { Success = true });
更改为
return Json(new { });
如何触发false而不是默认设置为true?
答案 1 :(得分:0)
试试这个:
$('#save').click(function () {
var CourseID = $('.form-control span1').val();
var Type = $('.form-control span2').val();
var id =@Model.UserId
$.ajax({
url: '@Url.Action("AdminUser/Edit")',
type: 'POST',
data: { id: '@Model.UserId', CourseID: CourseID, Type: Type },
Success: function (response) {
if (response.Success) {
alert("done");
}
else {
alert("Errror");
}
return false;
}
});
});
return false将停止页面的重定向 添加脚本包〜/ bundles / jqueryval