使用jQuery职位MVC 3不工作部署时, [英] Using jquery post for mvc 3 not working when deployed

查看:117
本文介绍了使用jQuery职位MVC 3不工作部署时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有了这个MVC 3应用程序,有我用填充通过jQuery的div下拉。它工作正常,在当地,但是当我将它部署到服务器时,它的重定向错误。这里是我的jQuery的:

  $(#ddlCategoryMain)。改变(函数(){
    $。员额(/首页/分类/,{mileID:$(本).VAL()},功能(数据){
        refreshDiv($(#DIV主),数据);
    });
});功能refreshDiv(选择,数据){
    select.html();
    select.append(数据);
}

本地能正常工作。但是,当部署到我的服务器似乎是在寻找 HTTP:// MYSERVER /首页/分类代替的 HTTP:// MYSERVER / mywebsite /首页/分类

我可以通过简单地添加在jQuery函数到/ home /分类之前我的应用程序的名称修复它,但感觉不对......

我也试图添加../,〜/,../../前/主页但并没有区别。

这个小问题解决方案的任何?
谢谢!


解决方案

选项1

假设你的jQuery的方法是在你看来,你可以使用 Url.Action()


  

生成一个完全合格的URL到
  通过使用指定的操作方法
  操作名称和控制器的名称。


  $(#ddlCategoryMain)。改变(函数(){
    .post的$('<%= Url.Action(类别,家)%GT;',{mileID:$(本).VAL()},功能(数据){
        refreshDiv($(#主),数据);
    });
});

或者这样,如果你使用的是剃须刀

  $(#ddlCategoryMain)。改变(函数(){
    .post的$('@ Url.Action(类别,家)',{mileID:$(本).VAL()},功能(数据){
        refreshDiv($(#主),数据);
    });
});

选项2

如果该方法是在外部js文件,你可以在您的视图声明一个全局变量。

  VAR myUrl ='@ Url.Action(类别,家);

,然后在 $。POST

  $(#ddlCategoryMain)。改变(函数(){
    $。员额(myUrl,{mileID:$(本).VAL()},功能(数据){
        refreshDiv($(#主),数据);
    });
});

So I've got this MVC 3 application that has a dropdown that I use to populate a div via jquery. It works fine locally but when I deploy it to the server it's redirecting incorrectly. Here's my jquery:

$("#ddlCategoryMain").change(function () {
    $.post("/Home/Category/", { mileID: $(this).val() }, function (data) {
        refreshDiv($("div#main"), data);
    });
});

function refreshDiv(select, data) {
    select.html("");
    select.append(data);
}

Locally this works fine. But when deployed to my server it appears to be looking for http://myserver/Home/Category instead of http://myserver/mywebsite/Home/Category

I can fix it by simply adding the name of my application before the /Home/Category in the jquery function, but that doesn't feel right...

I've also tried to add ../, ~/, ../../ before the /Home but that made no difference.

Any solutions to this minor problem? Thanks!

解决方案

Option 1

Assuming your jQuery method is in your view you can use Url.Action()

Generates a fully qualified URL to an action method by using the specified action name and controller name.

$("#ddlCategoryMain").change(function () {
    $.post('<%=Url.Action("Category", "Home")%>', { mileID: $(this).val() }, function (data) {
        refreshDiv($("#main"), data);
    });
});

Or this if you are using razor

$("#ddlCategoryMain").change(function () {
    $.post('@Url.Action("Category", "Home")', { mileID: $(this).val() }, function (data) {
        refreshDiv($("#main"), data);
    });
});

Option 2

If the method is in an external js file you could declare a global variable in your view.

var myUrl = '@Url.Action("Category", "Home")';

and then in your $.post

$("#ddlCategoryMain").change(function () {
    $.post(myUrl , { mileID: $(this).val() }, function (data) {
        refreshDiv($("#main"), data);
    });
});

这篇关于使用jQuery职位MVC 3不工作部署时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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