7

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.

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.

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.

8 Answers 8

20

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

<add key="UnobtrusiveJavaScriptEnabled" value="true" />
Sign up to request clarification or add additional context in comments.

1 Comment

For some reason when installing the nuget package for Microsoft Unobtrusive Ajax this was not added to my Web.Config. Thanks for the tip.
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

Thanks, Arty

1 Comment

This seems to be the first thing that people should check - whether they are including the MicrosoftAjax.js and MicrosoftMvcAjax.js files
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.

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

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 Comments

@Sam - I Already took a look with firebug and can see both Ajax libraries are loaded when I click on the scripts tab. I am not that familiar with firebug, though, so I might be missing something. I have used fiddler a lot so I will take a look with it tomorrow when I get to the office to verify the call for the scripts are not failing. Since the ajax call is triggered from a submit button long after the page has rendered I would rule out that the scripts haven't had a chance to load. Thanks.
@Sam - took a look with Fiddler this morning and guess what? The path to the scripts file is incorrect for that page only. Don't know why yet, but when I put a fully qualified path in for the scripts, the page works perfectly. Thanks, I am marking your answer as correct since it sent me down the right path - using a tool that would have told me what I needed to know two days ago :)
3

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

Comments

1

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

Do the below steps:

  1. PM> Install-Package MicrosoftAjax

  2. PM> Install-Package MicrosoftMvcAjax.Mvc5

  3. Include them in bundleconfig like below:

    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.

Comments

0

Add to web.cofig in section:

 <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"/>

1 Comment

As stated above, the web.config file is correctly configured.
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.

Comments

0

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

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

Hope this helps someone.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.