ASP.NET MVC AJAX Sys是未定义的错误

时间:2022-11-06 16:37:19

I am getting a "Microsoft JScript runtime error: 'Sys' is undefined" error on one of my pages in an MVC application when I attempt an AJAX call. The AJAX call is made from a partial view which is embedded in more than one page. It works fine on all of the pages except one. I have read posts pointing to the web.config file settings and .axd mappings as possible solutions, but the application is configured correctly in the web.config, and the .axd mappings are also correct in IIS. Plus it works fine on all of the pages that use this partial view except one. It is acting like the AJAX libraries are not loading for this one page.

当我尝试AJAX调用时,我在MVC应用程序中的某个页面上收到“Microsoft JScript运行时错误:'Sys'未定义”错误。 AJAX调用是从部分视图中进行的,该视图嵌入在多个页面中。它可以在除一个页面之外的所有页面上正常工作。我已经阅读了指向web.config文件设置和.axd映射的帖子作为可能的解决方案,但是在web.config中正确配置了应用程序,并且IIS中的.axd映射也是正确的。此外,它可以在使用此局部视图的所有页面上正常工作,除了一个。它表现得像AJAX库没有加载这一页。

The references to the script files are in the shared site.master file. All of the pages, including the one that doesn't work, reference the same masterpage.

对脚本文件的引用位于共享的site.master文件中。所有页面(包括不起作用的页面)都引用相同的母版页。

Any ideas? I have been working on this one for two days now. Thanks.

有任何想法吗?我已经在这个工作了两天了。谢谢。

EDIT: As Sam pointed out below, it would seem like the AJAX call is firing before the AJAX libraries have a chance to load. But, the AJAX call is triggered by a submit button long after the page has rendered, so the AJAX libraries have had plenty of time to load - sorry for not giving enough information the first time.

编辑:正如Sam在下面指出的那样,似乎AJAX调用在AJAX库有机会加载之前就已经开始了。但是,AJAX调用是在页面呈现后很长时间由提交按钮触发的,因此AJAX库有足够的时间加载 - 抱歉第一次没有提供足够的信息。

8 个解决方案

#1


4  

Load the page in Firefox, then use Firebug to inspect the page - you will be able to see all the individual scripts that have been loaded, plus all the network requests that were issued, and whether they succeeded or not. This is better than trying to troubleshoot from the server perspective.

在Firefox中加载页面,然后使用Firebug检查页面 - 您将能够看到已加载的所有单个脚本,以及发出的所有网络请求,以及它们是否成功。这比尝试从服务器角度进行故障排除要好。

If you are using IE8, you can use the Developer Tools window, but I think Firebug is better - both tools support JavaScript debugging though.

如果您使用的是IE8,则可以使用Developer Tools窗口,但我认为Firebug更好 - 两种工具都支持JavaScript调试。

The most likely problem is that you are running script in your partial view before the scripts it is dependent on have had a chance to load - make sure that any script calls you have inside your partial view will only run once the page has loaded, and not immediately during loading.

最可能的问题是您在部分视图中运行脚本,然后它依赖的脚本才有可能加载 - 确保您在局部视图中调用的任何脚本只会在页面加载后运行,并且在装载期间不会立即

#2


17  

Just in case... use the following to avoid path issues

以防万一...使用以下内容来避免路径问题

<script src="<%= Url.Content("~/Scripts/MicrosoftAjax.debug.js") %>" 
    type="text/javascript"></script>  
<script src="<%= Url.Content("~/Scripts/MicrosoftMvcAjax.debug.js") %>" 
    type="text/javascript"></script>

Source: http://msdn.microsoft.com/en-us/library/dd381533.aspx

来源:http://msdn.microsoft.com/en-us/library/dd381533.aspx

Thanks, Arty

谢谢,阿蒂

#3


17  

In web.config add the following line of code under appsettings tag:

在web.config中,在appsettings标记下添加以下代码行:

<add key="UnobtrusiveJavaScriptEnabled" value="true" />

#4


3  

All the above cases are ok.But sometimes developer forget to add javascript files for ajax .So please check that also.

所有上述情况都没问题。但有时开发人员忘记为ajax添加javascript文件。所以请检查一下。

#5


0  

Add to web.cofig in section:

添加到web.cofig部分:

 <remove verb="*" path="*.asmx"/>

 <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

 <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

 <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>

 <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

#6


0  

Regarding your response to Sam, one thing I've noticed in a lot of MVC apps is that people don't know how to deal with the ambiguity of relative paths and the application/runtime. For example, the URL rewriting pretty much guarantees that a particular page can appear at different depths than you anticipated, so ../../images will point somewhere else depending on whether you're looking at /products/widget or /products/widget/12345, even though the view might be the same. As Arty pointed out, the best way to deal with this is to let the engine do the work for you by using a URL utility and application-relative paths that will be fixed up by the application regardless of the context.

关于你对Sam的回应,我在很多MVC应用程序中注意到的一件事是,人们不知道如何处理相对路径和应用程序/运行时的模糊性。例如,URL重写几乎可以保证特定页面可以出现在不同于您预期的深度,因此../../ images将指向其他位置,具体取决于您是在查看/ products / widget还是/ products / widget / 12345,即使视图可能相同。正如Arty指出的那样,解决这个问题的最佳方法是让引擎通过使用URL实用程序和应用程序相对路径为您完成工作,这些路径将由应用程序修复,而不管上下文如何。

#7


0  

I have also found that using the following works with ASP.NET MVC2.

我还发现使用以下工作与ASP.NET MVC2。

Instead of using MicrosoftMvcAjax.js, you use MicrosoftMvcValidation.js

您可以使用MicrosoftMvcValidation.js,而不是使用MicrosoftMvcAjax.js

Hope this helps someone.

希望这有助于某人。

#8


0  

Basically you might be missing: MicrosoftMvcAjax., MicrosoftMvcValidation.debug and MicrosoftMvcValidation JS file references.

基本上您可能会丢失:MicrosoftMvcAjax。,MicrosoftMvcValidation.debug和MicrosoftMvcValidation JS文件引用。

Do the below steps:

执行以下步骤:

  1. PM> Install-Package MicrosoftAjax

    PM>安装包MicrosoftAjax

  2. PM> Install-Package MicrosoftMvcAjax.Mvc5

    PM>安装包MicrosoftMvcAjax.Mvc5

  3. Include them in bundleconfig like below:

    将它们包含在bundleconfig中,如下所示:

    bundles.Add(new ScriptBundle("~/bundles/mvcFoolProof")
           .Include("~/Scripts/MicrosoftAjax*",
                    "~/Scripts/MicrosoftMvcAjax*", 
                    "~/Scripts/MicrosoftMvcValidation*",
                    "~/Scripts/mvcfoolproof*",
                    "~/Scripts/MvcFoolproofJQueryValidation*",
                    "~/Scripts/MvcFoolproofValidation*"));
    

Now it should work without any errors.

现在它应该没有任何错误。

#1


4  

Load the page in Firefox, then use Firebug to inspect the page - you will be able to see all the individual scripts that have been loaded, plus all the network requests that were issued, and whether they succeeded or not. This is better than trying to troubleshoot from the server perspective.

在Firefox中加载页面,然后使用Firebug检查页面 - 您将能够看到已加载的所有单个脚本,以及发出的所有网络请求,以及它们是否成功。这比尝试从服务器角度进行故障排除要好。

If you are using IE8, you can use the Developer Tools window, but I think Firebug is better - both tools support JavaScript debugging though.

如果您使用的是IE8,则可以使用Developer Tools窗口,但我认为Firebug更好 - 两种工具都支持JavaScript调试。

The most likely problem is that you are running script in your partial view before the scripts it is dependent on have had a chance to load - make sure that any script calls you have inside your partial view will only run once the page has loaded, and not immediately during loading.

最可能的问题是您在部分视图中运行脚本,然后它依赖的脚本才有可能加载 - 确保您在局部视图中调用的任何脚本只会在页面加载后运行,并且在装载期间不会立即

#2


17  

Just in case... use the following to avoid path issues

以防万一...使用以下内容来避免路径问题

<script src="<%= Url.Content("~/Scripts/MicrosoftAjax.debug.js") %>" 
    type="text/javascript"></script>  
<script src="<%= Url.Content("~/Scripts/MicrosoftMvcAjax.debug.js") %>" 
    type="text/javascript"></script>

Source: http://msdn.microsoft.com/en-us/library/dd381533.aspx

来源:http://msdn.microsoft.com/en-us/library/dd381533.aspx

Thanks, Arty

谢谢,阿蒂

#3


17  

In web.config add the following line of code under appsettings tag:

在web.config中,在appsettings标记下添加以下代码行:

<add key="UnobtrusiveJavaScriptEnabled" value="true" />

#4


3  

All the above cases are ok.But sometimes developer forget to add javascript files for ajax .So please check that also.

所有上述情况都没问题。但有时开发人员忘记为ajax添加javascript文件。所以请检查一下。

#5


0  

Add to web.cofig in section:

添加到web.cofig部分:

 <remove verb="*" path="*.asmx"/>

 <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

 <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

 <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>

 <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

#6


0  

Regarding your response to Sam, one thing I've noticed in a lot of MVC apps is that people don't know how to deal with the ambiguity of relative paths and the application/runtime. For example, the URL rewriting pretty much guarantees that a particular page can appear at different depths than you anticipated, so ../../images will point somewhere else depending on whether you're looking at /products/widget or /products/widget/12345, even though the view might be the same. As Arty pointed out, the best way to deal with this is to let the engine do the work for you by using a URL utility and application-relative paths that will be fixed up by the application regardless of the context.

关于你对Sam的回应,我在很多MVC应用程序中注意到的一件事是,人们不知道如何处理相对路径和应用程序/运行时的模糊性。例如,URL重写几乎可以保证特定页面可以出现在不同于您预期的深度,因此../../ images将指向其他位置,具体取决于您是在查看/ products / widget还是/ products / widget / 12345,即使视图可能相同。正如Arty指出的那样,解决这个问题的最佳方法是让引擎通过使用URL实用程序和应用程序相对路径为您完成工作,这些路径将由应用程序修复,而不管上下文如何。

#7


0  

I have also found that using the following works with ASP.NET MVC2.

我还发现使用以下工作与ASP.NET MVC2。

Instead of using MicrosoftMvcAjax.js, you use MicrosoftMvcValidation.js

您可以使用MicrosoftMvcValidation.js,而不是使用MicrosoftMvcAjax.js

Hope this helps someone.

希望这有助于某人。

#8


0  

Basically you might be missing: MicrosoftMvcAjax., MicrosoftMvcValidation.debug and MicrosoftMvcValidation JS file references.

基本上您可能会丢失:MicrosoftMvcAjax。,MicrosoftMvcValidation.debug和MicrosoftMvcValidation JS文件引用。

Do the below steps:

执行以下步骤:

  1. PM> Install-Package MicrosoftAjax

    PM>安装包MicrosoftAjax

  2. PM> Install-Package MicrosoftMvcAjax.Mvc5

    PM>安装包MicrosoftMvcAjax.Mvc5

  3. Include them in bundleconfig like below:

    将它们包含在bundleconfig中,如下所示:

    bundles.Add(new ScriptBundle("~/bundles/mvcFoolProof")
           .Include("~/Scripts/MicrosoftAjax*",
                    "~/Scripts/MicrosoftMvcAjax*", 
                    "~/Scripts/MicrosoftMvcValidation*",
                    "~/Scripts/mvcfoolproof*",
                    "~/Scripts/MvcFoolproofJQueryValidation*",
                    "~/Scripts/MvcFoolproofValidation*"));
    

Now it should work without any errors.

现在它应该没有任何错误。