[英]jQuery Ajax failing to call to MVC 4 Controller method
I'm attempting to use jQuery in order to fire off an Ajax call after clicking a certain button. 我试图使用jQuery来点击某个按钮后触发Ajax调用。 I've read several examples of the syntax and issues that may be encountered, but have failed to find a working solution for my cause.
我已经阅读了几个可能遇到的语法和问题的例子,但未能找到适合我的工作的解决方案。 Here's the code.
这是代码。
Controller Method: (HomeController.cs) 控制器方法:(HomeController.cs)
[HttpPost]
public JsonResult ChangeCompany(string companyCode)
{
return Json(new { result = companyCode }, JsonRequestBehavior.AllowGet);
}
jQuery Code: jQuery代码:
function changeCompany(company) {
$.ajax({
url: '@Url.Action("ChangeCompany", "Home")',
type: 'POST',
data: JSON.stringify({ companyCode: company }),
success: function (data) {
alert("Company: " + data);
},
error: function (req, status, error) {
alert("R: " + req + " S: " + status + " E: " + error);
}
});
}
And finally, I'm calling this function with: 最后,我正在调用此函数:
$('.companyButton').click(function () {
compCode = $(this).text();
debug("Click event --> " + $(this).text());
changeCompany(compCode);
});
My debug message displays properly, and the Ajax call constantly fails with the following alert: R: [object Object] S: error E: Not Found
我的调试消息显示正常,并且Ajax调用经常失败并显示以下警告:
R: [object Object] S: error E: Not Found
I'm not entirely sure what to make of that. 我不完全确定如何做到这一点。
I know there are several questions on this topic, but none of them seem to resolve my issue and I'm honestly not sure what's wrong with these code blocks. 我知道关于这个主题有几个问题,但是它们似乎都没有解决我的问题,而且老实说我不确定这些代码块有什么问题。 Any insight would be appreciated.
任何见解将不胜感激。
EDIT: In case it's worth noting, this is for a mobile device. 编辑:如果值得注意,这是一个移动设备。 Testing on Windows 8 Phone Emulator (Internet Explorer), alongside jQuery Mobile.
在Windows 8 Phone Emulator(Internet Explorer)和jQuery Mobile上进行测试。 Not sure if that affects Ajax at all
不确定这是否会影响Ajax
EDIT 2: After taking a look at the raw network call, it seems that 'Url.Action("ChangeCompany", "Home")'
is not being converted into the proper URL and is instead being called directly as if it were raw URL text. 编辑2:在查看原始网络调用之后,似乎
'Url.Action("ChangeCompany", "Home")'
没有被转换为正确的URL而是被直接调用,就好像它是原始URL一样文本。 Is this due to an outdated jQuery, or some other factor? 这是由于过时的jQuery还是其他一些因素?
Ok with your EDIT2 it seems you are using url: '@Url.Action("ChangeCompany", "Home")',
in a separate JavaScript file. 好的,你的EDIT2似乎是在一个单独的JavaScript文件中使用
url: '@Url.Action("ChangeCompany", "Home")',
。 you can only write the razor code inside the .cshtml file and it is not working inside the .js files 你只能在.cshtml文件中编写剃刀代码,它在.js文件中不起作用
You are missing some important parameters in your AJAX call. 您在AJAX调用中缺少一些重要参数。 Change your AJAX call as below:
更改您的AJAX调用如下:
function changeCompany(company) {
$.ajax({
url: '@Url.Action("ChangeCompany", "Home")',
type: 'POST',
data: JSON.stringify({ companyCode: company }),
success: function (data) {
alert("Company: " + data);
},
error: function (req, status, error) {
alert("R: " + req + " S: " + status + " E: " + error);
}
});}
You can then annotate your controller method with [HttpPost]
attribute as below; 然后,您可以使用
[HttpPost]
属性注释您的控制器方法,如下所示;
[HttpPost]
public JsonResult ChangeCompany(string companyCode)
{
return Json(new { result = companyCode }, JsonRequestBehavior.AllowGet);
}
Note that your action is not returning companyCode
directly. 请注意,您的操作不会直接返回
companyCode
。 You're assigning it to Json result
property therefore In your success function to display result you need to have: 您将它分配给Json
result
属性因此在您的成功函数中显示结果您需要:
success: function (data)
{
alert("Company: " + data.result);
}
Also this: E: Not Found
tells me that you may have some routing issues. 另外这个:
E: Not Found
告诉我你可能有一些路由问题。 If you set a break point inside of your ChangeCompany
Action, is it being hit ? 如果您在
ChangeCompany
Action中设置了一个断点,它是否被击中?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.