简体   繁体   English

通过url路由访问页面时的jQuery对象预期错误

[英]JQuery object expected error when accessing page via url routing

In my global.asax I have url routing setup like below: 在我的global.asax中,我的网址路由设置如下所示:

routes.MapPageRoute("User Logon", "{Vendor}/Logon", "~/Logon.aspx");

In the logon.aspx page, I have a script that "stylizes" the logon button: 在logon.aspx页面中,我有一个脚本,用于“样式化”登录按钮:

<link href="jquery/css/flick/jquery-ui-1.8.1.custom.css" rel="stylesheet" type="text/css" />
<link href="images/style.css" rel="stylesheet" type="text/css" />
<script src="jquery/js/jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="jquery/js/jquery-ui-1.8.1.custom.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $('#<%= ButtonLogon.ClientID %>').button();
    });
</script>

When I access the page us a url (in debug mode) http://localhost/logon.aspx?v=1 the page loads correctly and the jquery button command loads correctly. 当我访问页面时,我们使用一个URL(在调试模式下) http://localhost/logon.aspx?v = 1,该页面正确加载,并且jquery button命令正确加载。 But then I access the page using the new url route, I get this error. 但是,然后我使用新的URL路由访问页面时,出现此错误。

Microsoft JScript runtime error: Object expected Microsoft JScript运行时错误:预期对象

Anyone have an idea why this occurs? 有人知道为什么会这样吗?

Thanks. 谢谢。

That's because of relative paths in your HTML. 那是由于HTML中的相对路径。

When you access your page as http://your.domain/Logon.aspx , the relative URL jquery/js/jquery-1.4.2.min.js resolves to http://your.domain/jquery/js/jquery-1.4.2.min.js and loads correctly. 当您以http://your.domain/Logon.aspx访问页面时,相对URL jquery/js/jquery-1.4.2.min.js解析为http://your.domain/jquery/js/jquery-1.4.2.min.js并正确加载。

But when you access it as http://your.domain/xxx/Logon.aspx , that URL resolves to http://your.domain/xxx/jquery/js/jquery-1.4.2.min.js , and since there's really no folder named xxx on your server, the server returns 404 and the script fails to load. 但是,当您以http://your.domain/xxx/Logon.aspx访问时,该URL会解析为http://your.domain/xxx/jquery/js/jquery-1.4.2.min.js ,因为您的服务器上确实没有名为xxx文件夹,服务器返回404并且脚本无法加载。 Therefore, when you subsequently try to access functions and variables defined in that script, you get an error. 因此,当您随后尝试访问该脚本中定义的函数和变量时,会出现错误。

To fix this, you should either use absolute paths - ie /jquery/js/jquery-1.4.2.min.js (note the leading slash), or use the ResolveUrl (or Url.Content ) method to map the URL correctly - ie <%= Url.Content( "~/jquery/js/jquery-1.4.2.min.js" ) %> 要解决此问题,您应该使用绝对路径-即/jquery/js/jquery-1.4.2.min.js (注意斜杠),或使用ResolveUrl (或Url.Content )方法正确地映射URL-即<%= Url.Content( "~/jquery/js/jquery-1.4.2.min.js" ) %>

The latter option is preferable, since it does not depend on your application being hosted at the root of the domain. 后一种选项是更可取的,因为它不依赖于您的应用程序托管在域的根目录下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM