JavaScript not firing in C# MVC4 -
i have following code in view :
<script type="text/javascript"> function oncancelclick(e) { var jobid = e; var flag = confirm('you cancel job : ' + jobid + '. sure want cancel job?'); if (flag) { $.ajax({ url: '/job/canceljob', type: 'post', data: { jobid: jobid }, datatype: 'html', success: function (result) { alert('job ' + jobid + ' cancelled.'); document.location = "@url.action("index", "job")"; }, error: function () { alert('something went wrong. check log more information.'); } }); } return false; } </script>
in view have :
<input type="submit" id="cancelbutton" value="cancel" onclick="javascript: return oncancelclick(@model.id);" />
in controller have :
[httppost] public actionresult canceljob(int jobid) { try { logger.loginfo(string.format("<start> cancel-button clicked job : {0}", jobid), jobid); jobcommandservice.changestatus(jobid, 6); logger.loginfo(string.format("<end> cancel-button clicked 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 run in vs2012 works fine. when deploy server, i'm getting message went wrong. in logging there no trace of button being clicked.
as per comment, when deployed app installed in accindigoapps.blabla.lok/jobmonitor
.
however script has url hardcoded url: '/job/canceljob'
. mean:
- when debugging vs script work because request being sent url
http://localhost:xxx/job/canceljob
- however in production, request sent
http://accindigoapps.blabla.lok/job/canceljob
, missing jobmonitor part.
you need way inform js code base url of application:
you generate url in razor view using
url.action("canceljob","job")
, pass url javascript code.another option use
url.content("~/")
in javascript of base layout. helperurl.content("~/")
return application folder,/
in dev environment ,/jobmonitor/
when deployed. way have app root-relative url available script, can use build root-relative urls doing in script:<script> var myapp = {}; myapp.baseurl = '@url.content("~/")'; </script> //some other script yours able keep using root-relative urls as: $.ajax({ url: myapp.baseurl + 'job/canceljob', ...
if prefer generate full urls, follow similar approach. have @ this question
hope helps!
Comments
Post a Comment