[英]ASP.NET MVC 5 calling controller method from Javascript Error
I am attempting to call a controller method from javascript and I seem to be having trouble getting this to execute correctly. 我试图从javascript调用控制器方法,我似乎无法正确执行此操作。 I have very little experience with javascript and have followed other examples of how to do this from stackoverflow but I am still having some issues- if anyone can help that'd be fantastic.
我对javascript的经验很少,并且已经跟踪了如何从stackoverflow执行此操作的其他示例,但我仍然遇到一些问题 - 如果有人可以提供帮助那就太棒了。
Basically what I am trying to do is set a .data tag on a javascript object to the string returned by a method on the controller (this method calls a webservice that runs a SQL Server function). 基本上我要做的是将javascript对象上的.data标记设置为控制器上的方法返回的字符串(此方法调用运行SQL Server函数的Web服务)。 The method needs to be passed one parameter which is used in the function.
该方法需要传递一个在函数中使用的参数。
The code is below: 代码如下:
Javascript Code Javascript代码
for (var i = 0; i < stats.length; i++)
{
var stat = stats[i].data('id');
var color = CallService(stat);
this.node.fill = color;
}
JQuery Method JQuery方法
function CallService(id) {
$.ajax({
url: '@Url.Action("CallService", "NodeController")',
type: 'GET',
dataType: 'json',
cache: false,
data: { 'id': id },
success: function (color) {
return color;
},
error: function () {
alert('Error occured');
}
});
}
Controller Method 控制器方法
[HttpGet]
public JsonResult CallService(string id)
{
var idNum = Convert.ToInt32(id);
var StationService = new getStationStatus.Service1SoapClient("Service1Soap");
string color = StationService.getStationStatus(idNum);
return Json(color, JsonRequestBehavior.AllowGet);
}
the controller is called the NodeController- which is what I am referring to in url: call of ajax. 控制器被称为NodeController-这是我在url中所指的:调用ajax。
Basically what is happening is when i run the page, I first get an error saying it cannot set this.node.fill to a null value THEN I get the alert that an error occurred- like I said I am pretty inexperienced with javascript so I am honestly not even sure if it is calling the method in the correct order if i get an error on this.node.fill before I receive the error message from JQuery. 基本上发生的事情是当我运行页面时,我首先得到一个错误,说它无法将this.node.fill设置为空值然后我得到发生错误的警报 - 就像我说我对javascript缺乏经验所以我老实说,如果我在收到来自JQuery的错误消息之前在this.node.fill上收到错误,它是否正在以正确的顺序调用方法。
Any/all help or suggestions is greatly appreciated!!! 任何/所有的帮助或建议非常感谢!!!
If your controller class is NodeController
, use only "Node" for the controller name in Url.Action
: 如果你的控制器类是
NodeController
,使用控制器名存实亡“节点” Url.Action
:
url: '@Url.Action("CallService", "Node")',
You cannot synchronously return a value from an asynchronous function. 您无法从异步函数同步返回值。 Either pass a callback to
CallService
to be called on success or use jquery promise - http://api.jquery.com/promise/ 将回调传递给
CallService
以在成功时调用或使用jquery promise - http://api.jquery.com/promise/
We don't know what this.node.fill
is or where it is defined. 我们不知道
this.node.fill
是什么或它定义的位置。 Likely, this.node
is not defined at the time the assignment is executed. 可能的是,在执行赋值时没有定义
this.node
。
您无需使用控制器名称编写控制器
@Url.Action("CallService", "Node")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.