服务器上的AJAX功能成功 [英] AJAX Success Function on Server

查看:116
本文介绍了服务器上的AJAX功能成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这工作我开发的机器上,而不是在生产服务器上。我试图用Ajax来更新一些div,但他们没有更新,但其他部分正常工作。我使用的服务器上的IIS 6。
当我调试与萤火虫在服务器端的代码,它不打我添加到成功的函数的任何断点



脚本:



 函数updateServiceInfo(节点ID){
VAR ID = {ID:节点ID};
$阿贾克斯({
网址:'/ ServiceInfo / ServiceInfoPartial',
型:GET,
数据:ID,
数据类型:HTML,
成功:功能(数据){
$('#serviceInfoContent)HTML(数据);
},
错误:功能(请求错误){

}
});
}



控制器:

 公共类ServiceInfoController:控制器
{
公众的ActionResult ServiceInfo()
{
返回PartialView(ServiceInfo);
}

公众的ActionResult ServiceInfoPartial(字符串ID)
{
返回PartialView(ServiceInfoPartial);
}
}



查看:



serviceinfopartial

  @model字符串
< p>
Немаєопису< / P>



serviceinfo

 < DIV ID =serviceInfo> 
< DIV ID =ContainerPanel级=ContainerPanel>
< DIV ID =serviceInfoHeader级=collapsePanelHeader>
< DIV ID =dvHeaderText级=HeaderContent>
Описсервісу< / DIV>
< DIV ID =dvArrow级=ArrowClose>
< / DIV>
< / DIV>
< DIV ID =serviceInfoContent级=serviceInfoContent>

< / DIV>
< / DIV>
< / DIV>

这是在控制台中返回的响应是



  GET HTTP://本地主机/经理/ GetManagers节点ID = 563344 404未找到42ms 


解决方案

Ahhhhhhhhhhhhhh,另外一个硬编码网址:

 网址:'/ ServiceInfo / ServiceInfoPartial',

从不硬编码的url像这样的ASP.NET MVC应用程序。



始终使用网址助手生成它们:

 网址:'@ Url.Action(ServiceInfoPartial,ServiceInfo)',

如果这是一个单独的JavaScript文件,你不能使用网址助手干脆用HTML5数据 - *属性一些DOM元素上:

 < DIV ID =serviceInfo数据URL =@ Url.Action(ServiceInfoPartial,serviceInfo)> 

< / DIV>



,然后在你的JavaScript简单:

 网址:$('#serviceInfo')的数据(URL),

当你在IIS托管它的代码不起作用的原因是因为在IIS你可能在一个虚拟目录托管应用程序,以便正确的URL不再 / ServiceInfo / ServiceInfoPartial ,但 / YourAppName / ServiceInfo / ServiceInfoPartial 。这就是为什么你不应该硬编码的网址,并使用助手生成它们的原因=>这是因为助手处理这个案件。使用助手的另一个好处是,如果你以后决定改变路线的布局的Global.asax 你不会需要修改所有的JavaScript文件,等等。 。您的网址是同治在一个位置集中。


This works on my dev machine, but not on a production server. I am trying to update some divs with ajax, but they are not updated, though other parts work fine. I am using IIS 6 on the server. When I debug this code on the server side with firebug, it does not hit any breakpoints I add to the success function.

Script:

function updateServiceInfo(nodeId) {
        var id = { id: nodeId };
        $.ajax({
            url: '/ServiceInfo/ServiceInfoPartial',
            type: 'GET',
            data: id,
            dataType: 'html',
            success: function (data) {
                $('#serviceInfoContent').html(data);
            },
    error: function (request, error) {

    }
        });
    }

Controller:

 public class ServiceInfoController : Controller
    {
        public ActionResult ServiceInfo()
        {
            return PartialView("ServiceInfo");
        }

        public ActionResult ServiceInfoPartial(string id)
        {
            return PartialView("ServiceInfoPartial");
        }
    }

Views:

serviceinfopartial

@model string
<p>
    Немає опису</p>

serviceinfo

<div id="serviceInfo">
    <div id="ContainerPanel" class="ContainerPanel">
        <div id="serviceInfoHeader" class="collapsePanelHeader">
            <div id="dvHeaderText" class="HeaderContent">
                Опис сервісу</div>
            <div id="dvArrow" class="ArrowClose">
            </div>
        </div>
        <div id="serviceInfoContent" class="serviceInfoContent">

        </div>
    </div>
</div>

The response that is returned in the console is

GET http://localhost/Managers/GetManagers?nodeId=563344 404 Not Found 42ms

解决方案

Ahhhhhhhhhhhhhh, another hardcoded url:

url: '/ServiceInfo/ServiceInfoPartial',

Never hardcode urls like this in an ASP.NET MVC application.

Always use url helpers to generate them:

url: '@Url.Action("ServiceInfoPartial", "ServiceInfo")',

or if this is in a separate javascript file where you cannot use url helpers simply use HTML5 data-* attributes on some DOM element:

<div id="serviceInfo" data-url="@Url.Action("ServiceInfoPartial", "ServiceInfo")">
...
</div>

and then in your javascript simply:

url: $('#serviceInfo').data('url'),

The reason your code doesn't work when you host it in IIS is because in IIS you are probably hosting your application in a virtual directory so the correct url is no longer /ServiceInfo/ServiceInfoPartial but is /YourAppName/ServiceInfo/ServiceInfoPartial. That's the reason why you should never hardcode any url and use helpers to generate them => it's because helpers handle this cases. Another benefit of using helpers is that if you later decide to change the layout of your routes in Global.asax you won't need to modify all your javascript files, etc... Your url managment is centralized in a single location.

这篇关于服务器上的AJAX功能成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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