1

I have the following code:

$.ajax({
    type: "GET",
    url: "/Search/GetNewData"  //Controller/ActionMethod
   ---snip---
)};

This works fine when run via localhost, however when it is deployed, it cannot find the controller method. I think it's probably a routing problem? But, with only a small limited knowledge using ASP.net, I would appreciate some advice.

Thanks.

4
  • Is your app running in a Virtual Directory? Commented Aug 30, 2011 at 8:40
  • There might be a problem with virtual directories in you deployment environment? Fire up Fiddler and inspect the request being made and where it goes. Make sure you can issue a request to /Search/GetNewData Commented Aug 30, 2011 at 8:41
  • I'm not sure Bertrand. It has been deployed by a colleague on an internal works server. Commented Aug 30, 2011 at 8:41
  • Spoke with my team lead, and yes we are running on a virtual directory. Any tips on how this should be handled? Thanks. Commented Aug 30, 2011 at 8:45

3 Answers 3

3

You really don't want to specify the URL directly in your JavaScript.

You need to use helpers; otherwise if you change your routes, you'll have to rewrite all the URLS in your JavaScript code. Also, it won't work if your website is hosted in a IIS virtual directory (what seems to be the issue here).

You have a couple solutions here, if your JavaScript code is embedded inside a view, simply use

$.ajax({
    type: 'GET',
    url: '@Url.Action("GetNewData", "Search")'
)};

If it's inside an external JavaScript file, you could, for example, use HTML5 data-* attributes to share the URL to your JavaScript code.

For example:

<div id="foo" data-update-url="@Url.Action("GetNewData", "Search")">
</div>

Then the JavaScript code would be something like

var updateDiv = $('#foo');
$.ajax({
    type: 'GET',
    url: updateDiv.data('update-url'),
    success: function(data) {
       updateDiv.append(data);
    }
)};
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, this looks like an ideal solution, unfortunately I didn't set the project up to use HTML 5 semantics!
It doesn't really matter as long as you can access them with jQuery.
I get an error saying that the data-* attribute is not a valid member of div. I thought this may have been due to not having HTML5 semantics? Thanks.
Yeah, the validation schema is probably just set to HTML4.01, you can change that anytime, there's a drop down list somewhere in Visual Studio to do so.
I think I may need to update VS2010 then, as that option is not in the drop down. I think i'll need to grab SP1. Thanks.
2

The following snippet would work for both virtual directories and web sites.

var baseUri = '@Url.Content("~/")';
$.ajax({
    type: "GET",
    url: baseUri + "Search/GetNewData"  //Controller/ActionMethod
   ---snip---
)};

You can also define the baseUri variable in your layout (above all <script tags) to be able to use it in all included javascripts.

3 Comments

no, but the OPs problem seems to be root vs virtual dir. And it will work for that.
I am using a seperate JS file, and the razor syntax will not work there. Thanks anyway.
@Darren Young: Read my answer again, especially the last sentence
0

Install FireBug for FireFox. Open .NET panel and Console panels. Request the URL to see whether you get any errors. If none found, then learn to use a debugger. This is by no means a quick fix, but it will help you in the future to fix these sort of problems yourself. Good luck.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.