jQuery的阿贾克斯:从应用程序根参考MVC控制器网址 [英] jQuery Ajax: Reference MVC controller url from App root

查看:165
本文介绍了jQuery的阿贾克斯:从应用程序根参考MVC控制器网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 HTTP运行ASP.NET MVC Web应用程序://本地主机/ myappname 。从jQuery的,我做jQuery的$。阿贾克斯()调用返回基于某个用户操作部分景色。我通常在包含我通过Ajax调用该函数相同的控制器视图调用它。例如,在我的家庭控制器中的视图具有以下功能(效果很好):

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;
}

这上面的URL被解析为的http://本地主机/ myappname /主页/ GetNavigationTreePV 和局部视图正确返回

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

现在,我试图用昆特单元测试我的功能。在这个测试案例中,我只是想确认我得到的函数结束并返回true。我创建了一个 QUNIT 控制器和相应的视图(它加载我的单元测试JavaScript文件)。从包含我的单元测试的test.js文件,我尽量让到是在我家的看法,像上面的相同功能的调用。但是,因为我现在跑出来 QUNIT 控制器,该URL被解析为的http://本地主机/ myappname / qunit /主页/ GetNavigationTreePV

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.

我试图改变我的Ajax请求的URL为 /主页/ GetNavigationTreePV / (与preceding正斜杠),但是当我做,我得到以下网址的http://本地主机/ myappname /主页/ 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.

所以,要清楚,我想写的方式,将永远从我的MVC应用程序的根目录开始我的Ajax请求,然后追加在我的$。阿贾克斯()函数给出的URL参数。

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?

推荐答案

我能够使用具有直接的JavaScript来完成这个 window.location.pathname 。请看下面的例子:

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;
}

这篇关于jQuery的阿贾克斯:从应用程序根参考MVC控制器网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆