jQuery Ajax: Reference MVC controller url from App root
Asked Answered
P

2

7

I have an ASP.NET MVC web application running from http://localhost/myappname. From jQuery, I make jQuery $.ajax() calls to return partial views based on some user action. I usually call this from a view in the same controller that contains the function I am calling via Ajax. For example, a view in my Home controller has the following function (which works well):

function loadMyPartialView() {
    $.ajax({
      type: 'GET',
      url: 'Home/GetNavigationTreePV/',
      success: function (data) { onSuccess(data); },
      error: function (errorData) { onError(errorData); }
    });
    return true;
}

This above url gets resolved to http://localhost/myappname/Home/GetNavigationTreePV and the partial view is returned correctly.

Now, I am trying to use qUint to unit test my functions. In this test case, I just want to verify I get to the end of the function and return true. I have created a QUNIT controller and corresponding view (which loads my unit test JavaScript file). From the test.js file that contains my unit tests, I try to make calls to the same functions that are in my Home views, like the one above. But, since I am now running out of the QUNIT controller, the url gets resolved to http://localhost/myappname/qunit/Home/GetNavigationTreePV.

I have tried changing the url of my ajax request to /Home/GetNavigationTreePV/ (with the preceding forward slash), but when I do that I get the following url http://localhost/myappname/Home/GetNavigationTreePV.

So, to be clear, I am trying to write my ajax request in a way that will always start from the root of my MVC application, and then append the url parameter given in my $.ajax() function.

Is there an easy way to do that? Am I going about this in a weird way?

Predation answered 10/10, 2011 at 13:55 Comment(0)
P
10

I was able to accomplish this with straight JavaScript using window.location.pathname. See the following example:

function loadMyPartialView() {
    $.ajax({
      type: 'GET',
      url: window.location.pathname + 'Home/GetNavigationTreePV/',
      success: function (data) { onSuccess(data); },
      error: function (errorData) { onError(errorData); }
    });
    return true;
}
Predation answered 17/10, 2011 at 16:18 Comment(1)
This is exactly what I was looking for as well. I was facing problem when deploying my app on IIS, in that case the url mentioned in the js file was not resolving correctly.Chromatograph
J
11

I think in your MVC View page you need @Url.Action

   function loadMyPartialView() {
        $.ajax({
          type: 'GET',
          url: '@Url.Action("GetNavigationTreePV", "Home")',
          success: function (data) { onSuccess(data); },
          error: function (errorData) { onError(errorData); }
        });
        return true;
    }

Alternatively you can use @Url.Content

   function loadMyPartialView() {
        $.ajax({
          type: 'GET',
          url: '@Url.Content("~/home/GetNavigationTreePV")',
          success: function (data) { onSuccess(data); },
          error: function (errorData) { onError(errorData); }
        });
        return true;
    }

If this is in a .js file, you can pass in the Url like

loadMyPartialView('@Url.Action("GetNavigationTreePV", "Home")')

Jamikajamil answered 10/10, 2011 at 14:21 Comment(4)
No luck. In that case, the URL was resolved as: localhost/ctsw/qunit/@Url.Action ....Predation
Ahh ok ... in this case, I think I may need to do this from a .js file in order to make use of the qUnit testing framework. Let me see of I can write unit tests in the view.Predation
You can pass in the url into the javascript e.g. loadMyPartialView('@Url.Action("GetNavigationTreePV", "Home")')Jamikajamil
That was a thought of mine as well, but I am looking for a way to not do that first. That is a last resort type of thing. I do appreciate the input though!Predation
P
10

I was able to accomplish this with straight JavaScript using window.location.pathname. See the following example:

function loadMyPartialView() {
    $.ajax({
      type: 'GET',
      url: window.location.pathname + 'Home/GetNavigationTreePV/',
      success: function (data) { onSuccess(data); },
      error: function (errorData) { onError(errorData); }
    });
    return true;
}
Predation answered 17/10, 2011 at 16:18 Comment(1)
This is exactly what I was looking for as well. I was facing problem when deploying my app on IIS, in that case the url mentioned in the js file was not resolving correctly.Chromatograph

© 2022 - 2024 — McMap. All rights reserved.