javascript jQuery,在运行 jQuery 语句时出现“500 内部服务器错误”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10323237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery, getting "500 Internal Server Error" when running a jQuery statement
提问by Brook Julias
Whenever the jQuery is triggered I recieve the error 500 Internal Server Error
, does anyone have any ideas why the code below might be causing this?
每当触发 jQuery 时,我都会收到错误消息500 Internal Server Error
,有没有人知道为什么下面的代码可能会导致这种情况?
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script src="../meta/js/jquery-1.7.2.js"></script>
<script>
$(document).ready(function() {
$('#form1').submit(function(e){
e.preventDefault();
alert('I clicked it');
});
});
</script>
</head>
<body>
<form name="form1" method="post">
<button id="button">grab user data</button>
<select></select>
</form>
</body>
</html>
回答by Jakub
I'm gonna guess its because of this variable:
我猜是因为这个变量:
<%=button.ClientID %>
There is nothing else (shown) that would signify anything else that would throw an error.
没有其他东西(显示)可以表示任何其他会引发错误的东西。
Can you clarify what you mean by 'jQuery is triggered'? Does that mean upon 'click' ? Or just by loading this page (I assumed the later)
你能澄清你所说的'jQuery被触发'是什么意思吗?这是否意味着“点击”?或者只是通过加载这个页面(我假设后者)
回答by Jonathan Payne
I would guess that it's giving the error with this line:
我猜它在这一行给出了错误:
$('#<%=button.ClientID %>')
Hard code a value instead and test it:
硬编码一个值并测试它:
$('#buttonID')
回答by aaberg
Your button is inside a tag with action="". This will cause a postback to "/". If your server doesn't handle this correctly, you will get an internal server error.
您的按钮位于带有 action="" 的标签内。这将导致回发到“/”。如果您的服务器没有正确处理此问题,您将收到内部服务器错误。
if you only want the alert to show, you can call preventDefault on the jQuery event object. Se example below:
如果您只想显示警报,则可以在 jQuery 事件对象上调用 preventDefault。下面的例子:
$(document).ready(function() {
$('#<%=button.ClientID %>').click( function(e) {
e.preventDefault();
alert("i clicked it");
});
});
$(document).ready(function() {
$('#<%=button.ClientID %>').click( function(e) {
e.preventDefault();
alert("i clicked it");
});
});
EDIT:Forget what i wrote above. It doesn't work. Instead, use the submit event on the form. Like this:
编辑:忘记我上面写的内容。它不起作用。相反,使用表单上的提交事件。像这样:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script src="@{'/public/javascripts/jquery-1.6.4.min.js'}" type="text/javascript" charset="${_response_encoding}"></script>
<script>
$(document).ready(function() {
$('#form1').submit(function(e){
e.preventDefault();
alert('I clicked it');
});
});
</script>
</head>
<body>
<form id="form1" method="post" action="">
<button id="button">grab user data</button>
<select></select>
</form>
</body>
</html>
回答by marcos.borunda
In order to achieve what you want this will do the job:
为了实现你想要的,这将完成这项工作:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
$('#myButton').on('click',function(){
alert("i clicked it");
});
});
</script>
</head>
<body>
<button id="myButton">Click Me!</button>
</body>
</html>
$(document).ready
is used to execute your code just after your page is rendered, then $('myButton')
will get any element with an id "myButton", then you will use the method "on" to attach any event you want, in this case you will want to choose the "click" event, then you should give an anonymous function where you will put the code you would like to execute on that action.
$(document).ready
用于在您的页面呈现后立即执行您的代码,然后$('myButton')
将获取任何带有 id“myButton”的元素,然后您将使用“on”方法附加您想要的任何事件,在这种情况下,您将要选择“click”事件,那么您应该提供一个匿名函数,您可以在其中放置要对该操作执行的代码。
回答by user2560528
you are attaching the submit
event to #form1
but your form doesn't have an id, only a name. You must select the form by name, $("form[name='form1']")
or give it an id of id="form1"
.
您将submit
事件附加到,#form1
但您的表单没有 ID,只有名称。您必须按名称选择表单,$("form[name='form1']")
或为其指定 id id="form1"
。