毫不夸张的AJAX错误:“未捕获的ReferenceError:未定义Sys" [英] Unobtrusive AJAX error: "Uncaught ReferenceError: Sys is not defined"

查看:90
本文介绍了毫不夸张的AJAX错误:“未捕获的ReferenceError:未定义Sys"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例代码: 在Visual Studio的MVC 5项目中,我设置了以下控制器和视图:

Example code: In an MVC 5 project in Visual Studio, I have set up the following controller and views:

控制器

TestController.cs

public class TestController : BaseController
{
    public ActionResult Index()
    {
        return View("Index");
    }

    public PartialViewResult MyPartialView1()
    {
        return PartialView("PartialView1");
    }

    public PartialViewResult MyPartialView2()
    {
        return PartialView("PartialView2");
    }
}

观看次数

Index.cshtml

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <script src="@Url.Content("~/Scripts/jquery-2.1.1.min.js")"></script>
    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")"></script>
</head>
<body>
    <h1>Testing Unobtrusive AJAX</h1>
    @Ajax.ActionLink("Partial 1", "MyPartialView1", "Test",
                new AjaxOptions() { UpdateTargetId = "contentShouldLoadHere", InsertionMode = InsertionMode.Replace })
    |
    @Ajax.ActionLink("Partial 2", "MyPartialView2", "Test",
                new AjaxOptions() { UpdateTargetId = "contentShouldLoadHere", InsertionMode = InsertionMode.Replace })
    <div id="contentShouldLoadHere">
         <p>This will get replaced.</p>
    </div>
</body>
</html>

PartialView1.cshtml

<h1>Test 1</h1>

PartialView2.cshtml

<h1>Test 2</h1>

问题

如果我正确理解,请单击"Partial 1",将"PartialView1"的内容加载到ID为"contentShouldLoadHere"的HTML元素中.相反,单击"Partial 1"会导致MyPartialView1操作(~/Test/MyPartialView1))的正常页面加载,并且在Chrome开发工具控制台中,我收到以下JavaScript错误:

If I have understood correctly, clicking "Partial 1" should load the contents of "PartialView1" into the HTML element with an ID of "contentShouldLoadHere". Instead, clicking "Partial 1" causes a normal page load of the MyPartialView1 action (~/Test/MyPartialView1), and in the Chrome dev tools console I get the following JavaScript error:

Uncaught ReferenceError: Sys is undefined

有人知道这是为什么吗,我该如何解决?

Does anyone know why this is, and how I can fix it?

注释 -我已通过Chrome开发者工具确认两个脚本均已正确加载. -@Ajax帮助程序生成的链接的HTML为:

Notes - I have confirmed via Chrome Dev Tools that both scripts are being correctly loaded. - The HTML for the link generated by the @Ajax helper is:

<a href="/Test/MyPartialView1" onclick="Sys.Mvc.AsyncHyperlink.handleClick(this, new Sys.UI.DomEvent(event), { insertionMode: Sys.Mvc.InsertionMode.replace, updateTargetId: &#39;contentShouldLoadHere&#39; });">Partial 1</a>

推荐答案

如果禁用了不引人注意的javascript,则会生成您为链接生成的html.您可以使用以下视图在视图中启用它:

The html your generating for the link is generated if unobtrusive javascript has been disabled. You can enable it in the view with

@{HtmlHelper.UnobtrusiveJavaScriptEnabled = true;}

或针对应用程序(在web.config中)

or for the application (in web.config) with

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

您呈现的html应该看起来像

Your rendered html should then look like

<a data-ajax="true" data-ajax-mode="replace" data-ajax-update="#contentShouldLoadHere" href="/Test/Partial1">Partial 1</a>

,并将与jquery.unobtrusive-ajax.js文件一起使用

这篇关于毫不夸张的AJAX错误:“未捕获的ReferenceError:未定义Sys"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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