页面在加载时不运行jquery脚本

时间:2022-10-12 18:08:44

I have an ASP.net mvc page that executes a jquery script on load. The script calls an action on a controller and hydrates a dropdown list.

我有一个ASP.net mvc页面,在加载时执行jquery脚本。该脚本调用控制器上的操作并保存下拉列表。

this works on my dev machine but when deployed to the webserver (Win 2k3 box running IIS 6) the page loads but it does not run the script resulting in an empty drop down list.

这适用于我的开发机器但是当部署到网络服务器(运行IIS 6的Win 2k3盒子)时,页面会加载,但它不会运行脚本,从而产生一个空的下拉列表。

I have the jquery-1.3.2.js file in the scripts folder, and I have added the mapping to aspnet_isapi.dll on the webserver. is there anything else that i am missing?

我在scripts文件夹中有jquery-1.3.2.js文件,我已经在webserver上添加了aspnet_isapi.dll的映射。还有什么我想念的吗?

this is the part of the page that hydrates the drop down lists that works on my machine but not on the webserver that it is deployed to as you can see the script call the ApplicationSettings controller to get a JSON object that hydrates the drop down list

这是页面的一部分,它可以保存在我的机器上运行的下拉列表,但不会在它部署到的Web服务器上,因为您可以看到脚本调用ApplicationSettings控制器来获取一个JSON对象来保护下拉列表

    <asp:Content ID="MainContent" ContentPlaceHolderID="MainContent" runat="server">
    <script src="~/Scripts/jquery-1.3.2.js" type="text/javascript"></script>
    <script type="text/javascript">
        // Wait for the document to be ready
        $(document).ready(function()
        {
            var selectedApp = $('#selectedApplication').val();
            var selectedMac = $('#selectedMachine').val();

            // Get the list of applications and populate the applications drop down list
            $.getJSON("/ApplicationSettings/Applications/List", function(data)
            {
                var items = "<option>----------- Select Application to Configure ----------</option>";
                $.each(data, function(i, application)
                {
                    var selected = (application.Value == selectedApp) ? 'selected' : '';
                    items += "<option value='" + application.Value + "'" + selected + ">" + application.Text + "</option>";
                });
                $("#Applications").html(items);
            });

            // Get the list of machines where the selected application is installed and populate the machines drop down list
            $("#Applications").change(function()
            {
                if ($("#Applications").attr("value") != "")
                {
                    // Enable the Machines DDL if a valid application is selected
                    $("#Machines").removeAttr("disabled");

                    // Populate the machines DDL with a list of machines where the selected application is installed
                    $.getJSON("/ApplicationSettings/Machines/List/" + $("#Applications > option:selected").attr("value"), function(data)
                    {
                        // Set the first item in the list
                        var items = "<option>---------- Select Machine -----------</option>";

                        // Retrieve the list of machines for th selected application from the database
                        $.each(data, function(i, machine)
                        {
                            var selected = (machine.Value == selectedMac) ? 'selected' : '';
                            items += "<option value='" + machine.Value + "'" + selected + ">" + machine.Text + "</option>";
                        });

                        // Add the items retrieved to the Machines DDL
                        $("#Machines").html(items);

                        if ($("#Machines").attr("value") != "")
                        {
                            $("#btnSearch").removeAttr("disabled");
                        }
                        else
                        {
                            $("#btnSearch").attr("disabled", "disabled");
                        }
                    });
                }
                else
                {
                    // If a valid application has not been selected then disable the Machines DDL
                    $("#Machines").attr("disabled", "disabled");
                    $("#btnSearch").attr("disabled", "disabled");
                }
            });

            if (selectedApp != "")
            {
                $("#Machines").removeAttr("disabled");

                $.getJSON("/ApplicationSettings/Machines/List/" + selectedApp, function(data)
                {
                    var items = "<option>---------- Select Machine -----------</option>";
                    $.each(data, function(i, machine)
                    {
                        var selected = (machine.Value == selectedMac) ? 'selected' : '';
                        items += "<option value='" + machine.Value + "'" + selected + ">" + machine.Text + "</option>";
                    });
                    $("#Machines").html(items);
                });

                if (selectedMac != "")
                {
                    $("#btnSearch").removeAttr("disabled");
                }
                else
                {
                    $("#btnSearch").attr("disabled", "disabled");
                }
            }
            else
            {
                $("#Machines").attr("disabled", "disabled");
                $("#btnSearch").attr("disabled", "disabled");
            }
        });


        function saveSelectedApplication()
        {
            $("#selectedApplication").val("");
            $("#selectedMachine").val("");
            $("#selectedApplication").val($("#Applications").attr("value"));
            if ($("#Applications").attr("value") != "")
            {
                $("#Machines").removeAttr("disabled");
                if ($("#Machines").attr("value") != "")
                {
                    $("#btnSearch").removeAttr("disabled");
                }
                else
                {
                    $("#btnSearch").attr("disabled", "disabled");
                }
            }
            else
            {
                $("#Machines").attr("disabled", "disabled");
                $("#btnSearch").attr("disabled", "disabled");
            }
        }

        function saveSelectedMachine()
        {
            $("#selectedMachine").val("");
            $("#selectedMachine").val($("#Machines").attr("value"));
            if ($("#Machines").attr("value") != "")
            {
                $("#btnSearch").removeAttr("disabled");
            }
            else
            {
                $("#btnSearch").attr("disabled", "disabled");
            }
        }
    </script>

3 个解决方案

#1


5  

I had an issue with script pathing. i used this extension method to sort it.

我遇到了脚本路径问题。我用这个扩展方法对它进行排序。

public static class HtmlHelperExtensions
    {
        /// <summary>
        /// Scripts the specified HTML to allow for correct pathing of the resource.
        /// </summary>
        /// <param name="html">The HTML.</param>
        /// <param name="path">The path.</param>
        /// <returns></returns>
        public static string Script(this HtmlHelper html, string path)
        {
            var filePath = VirtualPathUtility.ToAbsolute(path);
            return "<script type=\"text/javascript\" src=\"" + filePath + "\"></script>";
        }
    }

Then put this in the master page:

然后把它放在母版页中:

<%@ Import Namespace="MYNAMESPACE.Helpers" %>

and then jsut register all scripts like:

然后jsut注册所有脚本,如:

<%=Html.Script("~/Scripts/jquery-1.3.2.min.js")%>

Try implemeting the follwoing helper as well:

尝试实现以下帮助器:

public static string AbsolutePath(this HtmlHelper html, string path)
{
    return VirtualPathUtility.ToAbsolute(path);
}

and then change your call to

然后将你的电话改为

$.getJSON("<%=Html.AbsolutePath("~/ApplicationSettings/Machines/List/")%>"

When your view is rendered the absolute path should be inserted by MVC ViewEngine.

渲染视图时,MVC ViewEngine应插入绝对路径。

#2


0  

You put your code in a "$(document).ready(function() { [YOUR_CODE] });" block? If not, the DOM is probably not yet ready ;)

您将代码放在“$(document).ready(function(){[YOUR_CODE]});”中块?如果没有,DOM可能还没有准备好;)

#3


0  

How have you coded the action? This can be important if you run in a virtual directory on the server. If you run locally as http://localhost:xxxx/controller/action, but run remotely as http://mysever/myapp/controller/action, then you need to make sure that you're using Url.Action() to resolve the real path to the action result.

你是如何编码的?如果您在服务器上的虚拟目录中运行,这可能很重要。如果您在本地作为http:// localhost:xxxx / controller / action运行,但是以http:// mysever / myapp / controller / action远程运行,那么您需要确保使用Url.Action()解决动作结果的真实路径。

#1


5  

I had an issue with script pathing. i used this extension method to sort it.

我遇到了脚本路径问题。我用这个扩展方法对它进行排序。

public static class HtmlHelperExtensions
    {
        /// <summary>
        /// Scripts the specified HTML to allow for correct pathing of the resource.
        /// </summary>
        /// <param name="html">The HTML.</param>
        /// <param name="path">The path.</param>
        /// <returns></returns>
        public static string Script(this HtmlHelper html, string path)
        {
            var filePath = VirtualPathUtility.ToAbsolute(path);
            return "<script type=\"text/javascript\" src=\"" + filePath + "\"></script>";
        }
    }

Then put this in the master page:

然后把它放在母版页中:

<%@ Import Namespace="MYNAMESPACE.Helpers" %>

and then jsut register all scripts like:

然后jsut注册所有脚本,如:

<%=Html.Script("~/Scripts/jquery-1.3.2.min.js")%>

Try implemeting the follwoing helper as well:

尝试实现以下帮助器:

public static string AbsolutePath(this HtmlHelper html, string path)
{
    return VirtualPathUtility.ToAbsolute(path);
}

and then change your call to

然后将你的电话改为

$.getJSON("<%=Html.AbsolutePath("~/ApplicationSettings/Machines/List/")%>"

When your view is rendered the absolute path should be inserted by MVC ViewEngine.

渲染视图时,MVC ViewEngine应插入绝对路径。

#2


0  

You put your code in a "$(document).ready(function() { [YOUR_CODE] });" block? If not, the DOM is probably not yet ready ;)

您将代码放在“$(document).ready(function(){[YOUR_CODE]});”中块?如果没有,DOM可能还没有准备好;)

#3


0  

How have you coded the action? This can be important if you run in a virtual directory on the server. If you run locally as http://localhost:xxxx/controller/action, but run remotely as http://mysever/myapp/controller/action, then you need to make sure that you're using Url.Action() to resolve the real path to the action result.

你是如何编码的?如果您在服务器上的虚拟目录中运行,这可能很重要。如果您在本地作为http:// localhost:xxxx / controller / action运行,但是以http:// mysever / myapp / controller / action远程运行,那么您需要确保使用Url.Action()解决动作结果的真实路径。