Wednesday, 15 February 2012

How do I AJAX post to an MVC action from within a javascript file? -


The following code exists in the file

job.js . When I run this code against local code, everything behaves properly. When I do against an intranet server where there is an application name, I get 404.

  Job.updateJob = function () {$ .post ('/ Builder / ListJobItems', function (info) {...}); }  

I would love to use @ Url.Action () but as I said it is a javascript (.js) file. I know, where you put the data value on the body element, but for architectural reasons, I want to get this code decoupled from the DOM. This is a data acquisition engine, it should also know what a DOM is.

I would be fine with a solution that parses window.location in some code, but the problem is that I need to have a solution that works everywhere Will do

The reason for me being most disappointed is that I can not see how it will face every problem. Why is there no solution? Has everyone placed all their Javascript in Razor's view? Do not now separate the code in a module? I can not believe that none of these two are true, and yet there are no solutions coming to this. I am in loss.

What do I do during the need of data from server side in JS files?

I take action, and want to see with JS content. And instead job.js are also included

  & lt; Script type = "text / javascript" src = "@ Url.Action (" Job "," JSController ")" />  

And I've set everyone in the view, like

  Job.updateJob = function () {$ .post ('@ Url.Action ( "ListJobItems", "Builder"), Function (Data) {...}); }  

And it will work.


No comments:

Post a Comment