我是客户端-服务器编程概念的新手。我需要的是将四个 js 变量发送到我的 MVC 3 控制器操作。
$(document).ready(function() {
var $jcrop;
$('#image').Jcrop({
bgColor: 'red',
onSelect: refreshData
}, function () {
$jcrop = this;
});
function refreshData(selected) {
myData = {
x1: selected.x1,
x2: selected.x2,
y1: selected.y1,
y2: selected.y2
};
}
});
所以我在浏览器中获取了我的变量。
我在服务器端的是:
public ActionResult CreateCover(ImageCoordinates coordinates) {
ViewData.Model = coordinates;
return View();
}
public class ImageCoordinates
{
public int x1 { get; set; }
public int x2 { get; set; }
public int y1 { get; set; }
public int y2 { get; set; }
}
有没有一种简单的方法可以将我的四个参数从 js 发布到我的操作中?我尝试使用此站点中的几个示例,使用 $.ajax
像这样
$.ajax({
url: '/dashboard/createcover',
type: 'POST',
data: JSON.stringify(myData),
contentType: 'application/json; charset=utf-8',
success: function (data) {
alert(data.success);
},
error: function () {
alert("error");
},
async: false
});
但它没有用,我在我的行动中收到了 0 0 0 0。但老实说,我什至不确定如何调用这个 jquery 函数。我应该通过单击按钮来调用它,还是在我发布表单时自动调用它?还有其他方法可以发送此参数吗?
原文由 CadmusX 发布,翻译遵循 CC BY-SA 4.0 许可协议
这是解决方案 -
模型 -
行动 -
让我们创建一个视图,它将对 Action 进行 AJAX 调用。
输出 -