在JQuery的电话后相对URL [英] Relative URL in JQuery Post Call

查看:143
本文介绍了在JQuery的电话后相对URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的情况。

我开发我的第一个MVC应用程序Asp.Net。它在以下ADRESS运行我的服务器

I developed my first MVC Asp.Net application. it runs on my server at the following adress

http://localhost:59441/

我写了一些jQuery帖子中的方法看起来像这样

I wrote some JQuery Post Methods that looked like this

  $.ajax({
        type: "POST",
        url: "/CeduleGlobale/UpdateCheckBox", ...

CeduleGlobale是我ControllerName

UpdateCheckBox是我的方法名

CeduleGlobale is my ControllerName and UpdateCheckBox is my methodName

当我穿上TESTSERVER的应用程序,它被放在一个VirtualDirectory

When I put the Application on the testServer, it was put in a VirtualDirectory

因此​​,应用程序现在

hence the application is now

http://testServer/JprApplication/

没有更多的端口指定,并应用程序名称

no more port to specify and also an application Name

当我开始测试,我很快发现我的JQuery调用后没有工作了...

When I started to test, I quickly noticed my JQuery Post calls didn't work anymore...

我修改了他们所以现在的网址是

I modified them so now the URL is

/JprMvc/CeduleGlobale/UpdateCheckBox

问题是2倍。


  1. 这使得它很难测试我的开发机器上,因为IIS防爆preSS不允许我指定一个虚拟目录。

  2. 我不喜欢硬编码的虚拟目录的名称在JQuery的,因为我不知道该应用程序将在生产环境中什么样的名字,因此我将不得不修改我的剧本之前,我可以安装在生产中的应用。

我相信我缺少一些基本的东西进行简化。

I am sure I am missing some basic thing to simplify this.

感谢

推荐答案

根据你实际已经找到你的JavaScript在哪里(查看或单独的JS文件中),你有两个选择。

Depending on where you actually have your JavaScript located (inside the View or a separate JS file), you have a couple of options.

选项1 - 里面的视图

只需使用HTML辅助为您生成的链接

Simply use the Html Helpers to generate the links for you

<script type="text/javascript">
   $(function(){
        $.ajax({
           type: "POST",
           url: "@Url.Action("UpdateCheckBox", "CeduleGlobale")"
        });
   });
</script>

选项2 - 独立JS文件

我们通常具有设置了该页面的处理程序每​​页的功能。因此,我们可以做一些这样的:

We typically have a function per page that sets up that page's handlers. So, we can do something like the following:

查看

<script type="text/javascript">
    $(function(){
        SetOrderPage('@Url.Action("UpdateCheckBox", "CeduleGlobale")');
    });
</script>

独立JS文件

function SetOrderPage(ajaxPostUrl){
       $.ajax({
           type: "POST",
           url: ajaxPostUrl
       )};
}

选项3 - 独立JS文件方法2

您可以在你的全局变量在你的JS文件,它是siteroot。回到这里,平局是,你将需要手动创建你的每一个动作的方法路径。在每个页面上,你可以设置站点根目录全局变量这样:

You could have a global variable in your in your JS file that is the siteroot. The draw back here is that you will need to hand create each of your action method paths. On each page, you could set the site root global variable as such:

独立JS文件

var siteRoot;

查看

<script type="text/javascript">
    siteRoot = '@Server.MapPath("~/")';       
</script>

请你不能在一个独立的JS文件使用剃刀语法。我相信这是最让剃刀/ MVC / .NET动态地给你网站的路径或URL路径,因为它真的会减少,可能网站/虚拟目录之间移动时所犯的错误。

Keep in mind you cannot use Razor syntax in a stand alone JS file. I believe that it is best to let Razor/MVC/.NET dynamically give you the site path or URL route as it will really cut down on the mistakes that could be made when moving between sites/virtual directories.

这篇关于在JQuery的电话后相对URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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