从给定的第二个URL(这是行不通的)我假设你想用HttpGet
方法使用jQuery AJAX。
http://localhost:123/{controller}/{action}/{id}
这id
视为UrlParameter
:URL模式与这个路径相符。
因此就需要使用操作参数&数据表示URL的参数值,如下例所示:
控制器
[HttpGet]
public ActionResult EditRecord(int id)
{
// other stuff
}
视图(JS)
$.ajax({
url: "/Record/EditRecord",
type: "GET",
dataType: 'json', // response type
contentType: 'application/x-www-form-urlencoded; charset=utf-8', // header content type
data: { id: 1 },
processData: true, // this is important to use in GET methods
success: function (result) {
Function(result);
},
error: function (message) {
// throw error
}
});
替代地直接使用URL参数适用于GET方法而不指定data
内容:
视图(JS)
$.ajax({
url: "Record/EditRecord/1",
type: "GET",
processData: true, // this is important to use in GET methods
success: function (result) {
Function(result);
},
error: function (message) {
// throw error
}
});
NB:使用jQuery.get
简化版本:
$.get("/Record/EditRecord/1", function (result) {
Function(result);
}, "json").error(function (message) { // throw error
});
PS:这是HTTP的示例POST请求,如果你正在寻找适当的POST方法与AJAX。
控制器
[HttpPost]
public ActionResult EditRecord(Record rec)
{
// other stuff
}
视图(JS)
$.ajax({
url: "/Record/EditRecord",
type: "POST",
dataType: 'json', // response type
contentType: 'application/json; charset=utf-8', // header content type
data: JSON.stringify({ rec: { id: 1, name: 'XXXX', ... }}),
success: function (result) {
Function(result);
},
error: function (message) {
// throw error
}
});
参考:
Do GET, POST, PUT, DELETE in ASP.NET MVC with jQuery AJAX
两者的URL相同。什么是你得到的错误 –
对不起,我已经编辑了第二个网址。谢谢。 – MCarmona
'http:// localhost:123/Record/EditRecord/1' =>此请求是否转到GET方法?我知道如何使用'$ .ajax'来使用各种HTTP方法,但我需要确定你想要使用什么方法。 –