[英]jQuery AJAX call to ASP.NET WebMethod errors constantly
I have a really simple AJAX method inside my Default.aspx.cs and it looks like this: 我的Default.aspx.cs中有一个非常简单的AJAX方法,它看起来像这样:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string GetDate()
{
return DateTime.Now.ToString();
}
}
And the Default.aspx looks like this: 而Default.aspx看起来像这样:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript" src="http://192.168.1.2/tizma.com/js/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
// Add the page method call as an onclick handler for the div.
$("#Result").click(function() {
$.ajax({
type: "POST",
url: "Default.aspx/GetDate",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: AjaxSucceeded,
error: AjaxFailed
});
});
});
function AjaxSucceeded(result)
{
alert(result.d);
}
function AjaxFailed(result)
{
alert(result.status + " " + result.statusText);
}
</script>
</head>
<body>
<div id="Result">Click me</div>
</body>
</html>
The problem is when I click the div the ajax error function is all that is ever called with a 200 status. 问题是,当我单击div时,ajax错误函数就是状态为200的所有函数。
What am I doing wrong? 我究竟做错了什么?
Not sure if it's causing the problem but you have a line which reads: 不知道这是否是导致问题的原因,但是您有一行显示:
data: "{}",
it should read: 它应显示为:
data: {},
or you can omit the line altogether as it's an optional parameter to the method call. 或者您可以完全省略该行,因为它是方法调用的可选参数。 You are currently setting it to a string value when it actually expects parameters for the webmethod which might cause problems.
当前,当您实际上希望使用webmethod的参数(可能会导致问题)时,将其设置为字符串值。
Also, the lines reading: 另外,这些行显示为:
contentType: "application/json; charset=utf-8",
dataType: "json",
seem unnecessary to me because for starters, it isn't obvious that your webmethod is actually returning json. 对我来说似乎不必要,因为对于初学者来说,您的web方法实际上并不返回json并不明显。 I think it's just returning a string.
我认为这只是返回一个字符串。 Try removing those three lines alltogether and see if it doesn't just start working.
尝试一起删除这三行,看看它是否还不开始工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.