【发布时间】:2019-12-17 10:20:32
【问题描述】:
我有一个从控制器类调用 MVC 操作的 Ajax 方法。
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
url: "/ajax/Updates/Message",
dataType: "json",
success: function (response) {
//data variable has been declared already
data = response;
},
complete: function () {
if (data !== "") {
$('#div1').text(window.location.path);
$('#div2').text(data);
}
},
});
[HttpGet]
public async Task < ActionResult > Message()
{
string d = "test string";
return Json(d, JsonRequestBehavior.AllowGet);
}
Ajax 方法中的“url”是对操作方法的调用。
如果我想在 Ajax 响应中返回实际页面 URL,而不是控制器/操作 url,该怎么办?
所以这个控制器没有视图或与之相关的任何东西,它更像是一个辅助类。当我在任何其他页面中使用 ajax 时,它不会返回该特定页面的 URL 路径(通过 'window.location.path),例如/Accounts/Summary ,而是返回 Updates/Message (参考控制器和操作)
【问题讨论】:
标签: ajax asp.net-mvc asp.net-mvc-4