Ajax 调用未到达控制器方法
Ajax call not reaching controller method
是的,有一大堆帖子都有类似的问题。我已尝试遵循其中的答案,但我的 ajax 调用仍未到达控制器方法。
控制器(SalesController.cs):
[HttpGet]
public JsonResult fillVarietiesSelect(int species_id)
{
String[] alist = {"a","b","c"};
return Json(alist, JsonRequestBehavior.AllowGet);
}
javascript:
$('#species-select').on('change', function () {
var species_id = $('#species-select').val();
console.log("species id selected " + species_id);
alert("species id selected " + species_id);
$('#variety-select').empty();
$.ajax({
type: 'GET',
url: '@Url.Action("fillVarietiesSelect", "Sales")',
data: {species_id : species_id},
success: function (result) {
alert(result);
}
});
});
on change 事件正在触发,并且弹出带有正确数据的警报。我在控制器方法中设置了一个断点,但似乎没有执行到那里。
尝试完全按照以下方式做
控制器:
[HttpGet]
public ActionResult receive2(string p)
{
ViewBag.name = p;
List<string> lst = new List<string>() { p };
return Json(lst,JsonRequestBehavior.AllowGet);
}
客户端
$.ajax({
type: "GET",
url: "Main/receive2", // the method we are calling
contentType: "application/json; charset=utf-8",
data: { "p": $("#txtname").val() },
dataType:"json",
success: function (result) {
alert("yes");
alert('Yay! It worked!tim' + result);
window.location = "http://google.com";
// Or if you are returning something
},
error: function (result) {
alert('Oh no aa :(' + result[0]);
}
});
我已经检查过它是否正常工作
您的 404 错误告诉您 @Url.Action 未被视图引擎解析。 @ 是 Razor 语法,因此要使其正常工作,您需要为 C# 视图使用 .cshtml 扩展名或为 VB 视图使用 .vbhtml 并使用 MVC 3 或更高版本。
我遇到了同样的问题,将参数从 int
更改为 string
解决了问题。
在我的服务器端,
public string RemoveByDocumentID(int DocumentID)
将其更改为
public string RemoveByDocumentID(string DocumentID)
已解决问题。
在相同的上下文中,我有另一个同名方法(重载方法)。这也是导致问题的原因,所以我将方法名称设为唯一。
是的,有一大堆帖子都有类似的问题。我已尝试遵循其中的答案,但我的 ajax 调用仍未到达控制器方法。
控制器(SalesController.cs):
[HttpGet]
public JsonResult fillVarietiesSelect(int species_id)
{
String[] alist = {"a","b","c"};
return Json(alist, JsonRequestBehavior.AllowGet);
}
javascript:
$('#species-select').on('change', function () {
var species_id = $('#species-select').val();
console.log("species id selected " + species_id);
alert("species id selected " + species_id);
$('#variety-select').empty();
$.ajax({
type: 'GET',
url: '@Url.Action("fillVarietiesSelect", "Sales")',
data: {species_id : species_id},
success: function (result) {
alert(result);
}
});
});
on change 事件正在触发,并且弹出带有正确数据的警报。我在控制器方法中设置了一个断点,但似乎没有执行到那里。
尝试完全按照以下方式做
控制器:
[HttpGet]
public ActionResult receive2(string p)
{
ViewBag.name = p;
List<string> lst = new List<string>() { p };
return Json(lst,JsonRequestBehavior.AllowGet);
}
客户端
$.ajax({
type: "GET",
url: "Main/receive2", // the method we are calling
contentType: "application/json; charset=utf-8",
data: { "p": $("#txtname").val() },
dataType:"json",
success: function (result) {
alert("yes");
alert('Yay! It worked!tim' + result);
window.location = "http://google.com";
// Or if you are returning something
},
error: function (result) {
alert('Oh no aa :(' + result[0]);
}
});
我已经检查过它是否正常工作
您的 404 错误告诉您 @Url.Action 未被视图引擎解析。 @ 是 Razor 语法,因此要使其正常工作,您需要为 C# 视图使用 .cshtml 扩展名或为 VB 视图使用 .vbhtml 并使用 MVC 3 或更高版本。
我遇到了同样的问题,将参数从 int
更改为 string
解决了问题。
在我的服务器端,
public string RemoveByDocumentID(int DocumentID)
将其更改为
public string RemoveByDocumentID(string DocumentID)
已解决问题。
在相同的上下文中,我有另一个同名方法(重载方法)。这也是导致问题的原因,所以我将方法名称设为唯一。