简体   繁体   English

JavaScript无法在C#MVC4中触发

[英]JavaScript not firing in C# MVC4

I have the following code in my view : 我认为以下代码如下:

    <script type="text/javascript">
    function OnCancelClick(e)
    {
        var jobId = e;
        var flag = confirm('You are about to cancel job : ' + jobId + '. Are you sure you want to cancel this job?');
        if (flag) {
            $.ajax({
                url: '/job/CancelJob',
                type: 'POST',
                data: { jobId: jobId },
                dataType: 'html',
                success: function (result) { alert('Job ' + jobId + ' was cancelled.'); document.location = "@Url.Action("Index", "Job")"; },
                error: function () { alert('Something went wrong. Check the log for more information.'); }
        });
    }
    return false;
    }
</script>

In my view I also have : 我认为我也有:

<input type="submit" id="cancelButton" value="Cancel" onclick="javascript: return OnCancelClick(@Model.Id);" />

In my controller I have : 在我的控制器中,我有:

[HttpPost]
        public ActionResult CancelJob(int jobId)
        {
            try
            {
                logger.LogInfo(string.Format("<start> Cancel-button clicked for job : {0}", jobId), jobId);

                JobCommandService.ChangeStatus(jobId, 6);

                logger.LogInfo(string.Format("<end> Cancel-button clicked for job : {0}", jobId), jobId);

                return RedirectToAction("Index", "Job");
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message, ex, jobId);
                Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                return Json(new { Success = false, Message = ex.Message });
            }
        }

When I run this in my VS2012 it works just fine. 当我在VS2012中运行它时,它可以正常工作。 When I deploy it to the server, I'm getting the message that something went wrong. 当我将其部署到服务器时,我得到的消息是出了点问题。 In my logging there is no trace of the button being clicked. 在我的日志记录中,没有单击按钮的痕迹。

As per your comment, when deployed your app is installed in accindigoapps.blabla.lok/jobmonitor . 根据您的评论,部署后,您的应用程序将安装在accindigoapps.blabla.lok/jobmonitor

However your script has the url hardcoded as url: '/job/CancelJob' . 但是,您的脚本的url硬编码为url: '/job/CancelJob' That will mean: 那将意味着:

  • when you are debugging from VS your script will work because the request is being sent to a url like http://localhost:XXX/job/CancelJob 当您从VS进行调试时,您的脚本将正常运行,因为请求已发送到类似http://localhost:XXX/job/CancelJob
  • however in production, the request will be sent to http://accindigoapps.blabla.lok/job/CancelJob , missing the jobmonitor part. 但是在生产中,该请求将发送到http://accindigoapps.blabla.lok/job/CancelJob ,缺少工作监视器部分。

You need a way to inform your JS code about the base url of your application: 您需要一种方法来通知JS代码有关应用程序的基本URL:

  • You could generate the Url in a Razor view using Url.Action("CancelJob","job") and pass that Url into your javascript code. 您可以使用Url.Action("CancelJob","job")在Razor视图中生成Url,并将该Url传递到您的JavaScript代码中。

  • Another option would be to use Url.Content("~/") in some javascript of your base layout. 另一种选择是在基本布局的某些javascript中使用Url.Content("~/") That helper Url.Content("~/") will return only your application folder, / in your dev environment and /jobmonitor/ when deployed. 该助手Url.Content("~/")将只返回你的应用程序文件夹, /在你的开发环境和/jobmonitor/部署时。 That way you will have your app root-relative url available to any script, so you can use it to build root-relative urls as you were doing in your script: 这样,您将可以将应用程序的相对于根目录的URL用于任何脚本,因此您可以像在脚本中一样使用它来构建相对于根目录的URL:

     <script> var myApp = {}; myApp.BaseUrl = '@Url.Content("~/")'; </script> //Some other script like yours would be able to keep using root-relative urls as: $.ajax({ url: myApp.BaseUrl + 'job/CancelJob', ... 

If you prefer to generate full urls, you could follow a similar approach. 如果您希望生成完整的URL,则可以采用类似的方法。 Have a look at this question 看看这个问题

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM