简体   繁体   English

ASP.NET MVC 5从Javascript Error调用控制器方法

[英]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!!! 任何/所有的帮助或建议非常感谢!!!

  1. If your controller class is NodeController , use only "Node" for the controller name in Url.Action : 如果你的控制器类是NodeController ,使用控制器名存实亡“节点” Url.Action

     url: '@Url.Action("CallService", "Node")', 
  2. 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/

  3. 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.

相关问题 从JavaScript中正确调用控制器 - ASP.NET MVC - Calling the controller correctly from JavaScript - ASP.NET MVC ASP.Net MVC从FullCalendar的javascript函数调用控制器操作未加载视图 - ASP.Net MVC calling a controller action from FullCalendar's javascript function not loading the view jQuery不调用asp.net MVC中的控制器 - JQuery not calling controller in asp.net mvc javascript-如何将JSON对象中的数组从JavaScript传递给asp.net MVC控制器方法? - How to pass array in a JSON object from javascript to asp.net mvc controller method? ASP.NET MVC Kendo Grid 如何从 javascript 调用 controller 方法 - ASP.NET MVC Kendo Grid how to call controller method from javascript 从javascript函数获取数据到ASP.NET MVC Controller的操作方法 - Getting data from javascript function on to ASP.NET MVC Controller's Action Method 从ASP.NET MVC视图调用TypeScript类方法 - Calling a TypeScript class method from a ASP.NET MVC view 带有ajax的javascript函数的ASP.NET MVC调用服务器 - ASP.NET MVC calling server from javascript function with ajax 从 JavaScript 调用 ASP.NET MVC 操作方法 - Calling ASP.NET MVC Action Methods from JavaScript 将 javascript 数组从视图传递到 ASP.Net MVC 5 中的控制器 - Passing javascript array from view to controller in ASP.Net MVC 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM