0

I have following code in my Javascript file:

$.getJSON("/ProductMatrix/LocationList/" + $("#categoryTypeFilter > option:selected").attr("value"),
  function (data) {
    var items = "<option> Default  </option>";
    $.each(data,
      function (i, location) {
        items += "<option value=' " + location.Value + "'>" + location.Text + "</option>";
      });
    $("#captureLocationFilter").html(items);
  });

"/ProductMatrix/LocationList/" is controller/action but its not working on server, however it does work on Local machine. Looks like there is problem with url, as this path may not be existing on server. Can someone help me on it.

When I try Url.Action my external javascript file is not happy.

enter image description here

Chanchal
  • 47
  • 1
  • 11
  • Please carefully read the answer. `Url.Action` is a server method. It won't work in external js files. you need to build the url in your razor file and pass it to your js code as clearly explained in this post [How do I make JS know about the application root?](http://stackoverflow.com/questions/34360537/how-do-i-make-js-know-about-the-application-root) – Shyju Jul 13 '16 at 17:19

1 Answers1

0

My wild guess is that you are getting a 404 error. Check your browser console.

One suggestion is, do not hard code the url to your action method like that. Always try to use the html helper methods like Url.Action or Url.RouteUrl to build the correct relative url to your action method.

var url="@Url.Action("LocationList","ProductMatrix")";
var v=$("#categoryTypeFilter > option:selected").attr("value");
//update the url (add ? as needed) according to how your server expects the url params
$.getJSON(url + v , function (data) {              
             //do something with data    
});

This will work fine if your javascript code is inside a razor view, If it is inside an external javascript file, use the solution explained in this answer.

Community
  • 1
  • 1
Shyju
  • 214,206
  • 104
  • 411
  • 497
  • I tried that but looks like in my external javascript file is not liking it .Please find screen shot attached in question description. – Chanchal Jul 13 '16 at 17:13
  • Read the answer carefully. For external js file you should try the other approach i mentioned in the link – Shyju Jul 13 '16 at 17:14
  • Isn't it possible without angular? – Chanchal Jul 13 '16 at 17:24
  • It is. Check this one http://stackoverflow.com/questions/34427447/angularjs-directive-templateurl-doesnt-work-while-template-works/34427511#34427511 – Shyju Jul 13 '16 at 17:26