【发布时间】:2017-05-07 11:58:05
【问题描述】:
使用以下脚本,我试图访问使用 ajax 函数中的数据发送的变量,但我不能。
<script>
$('#inline-username').click(function () {
var comments = $('#inline-username').val();
//var selectedId = $('#hdnSelectedId').val();
$.ajax({
url: '@Url.Action("UpdateOrder")', // to get the right path to controller from TableRoutes of Asp.Net MVC
dataType: "json", //to work with json format
type: "POST", //to do a post request
contentType: 'application/json; charset=utf-8', //define a contentType of your request
cache: false, //avoid caching results
data: { test: $(this).text() }, // here you can pass arguments to your request if you need
success: function (data) {
// data is your result from controller
if (data.success) {
alert(data.message);
}
},
error: function (xhr) {
alert('error');
}
});
});
这是控制器中的动作
public ActionResult UpdateOrder()
{
// some code
var test = Request.Form["test"];
return Json(new { success = true, message = "Order updated successfully" }, JsonRequestBehavior.AllowGet);
}
我试过Request.Form["test"],但它的值为空。我应该如何接收data的对象?
【问题讨论】:
-
您需要在 url: '@Url.Action("UpdateOrder","ControllerName")' 中指定控制器名称, 并使用 [HttpPost] 属性装饰您的操作.
标签: c# jquery asp.net-mvc asp.net-ajax