使用jQuery直接调用ASP.NET AJAX页面方法--500(内部服务器错误)
我正在通过ajax和 所以我试着做ff。从本网站: 在JS文件中:使用jQuery直接调用ASP.NET AJAX页面方法--500(内部服务器错误),jquery,asp.net,ajax,iis,webmethod,Jquery,Asp.net,Ajax,Iis,Webmethod,我正在通过ajax和 所以我试着做ff。从本网站: 在JS文件中: $(document).ready(function () { // Add the page method call as an onclick handler for the div. $("#Result").click(function () { $.ajax({ type: "POST", url: "WebForm1.aspx/GetDate", data:
$(document).ready(function () {
// Add the page method call as an onclick handler for the div.
$("#Result").click(function () {
$.ajax({
type: "POST",
url: "WebForm1.aspx/GetDate",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
// Replace the div's content with the page method's return.
$("#Result").text(msg.d);
}
});
});
}))
在aspx中:
<html>
<head>
<title>Calling a page method with jQuery</title>
<script type="text/javascript" src="Scripts/jquery-1.4.1.js"></script>
<script type="text/javascript" src="Scripts/JScript1.js"></script>
</head>
<body>
<div id="Result">Click here for the time.</div>
</body>
</html>
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public static string GetDate()
{
return DateTime.Now.ToString();
}
}
}
当我在本地PC上的VS studio上运行它时,它运行得很好;但是,一旦文件部署到IIS服务器中,当我单击容器时会出现以下错误:
职位http:///WebForm1.aspx/GetDate 500(内部服务器错误)
IIS中是否有要配置的内容?还是与代码相关的错误 尝试删除这一行
[ScriptMethod(UseHttpGet=true)]
因为这一行是在您尝试Get请求时使用的,当您看到示例时,他们正在尝试执行POST请求,而这一行是不需要的
[WebMethod]
public static string GetDate()
{
return DateTime.Now.ToString();
}
如果要对GET方法执行POST请求,请尝试将javascript更改为:
$("#Result").click(function () {
$.get("WebForm1.aspx/GetDate", function (msg) {
// Replace the div's content with the page method's return.
$("#Result").text(msg.d);
});
});
这一点很好,但正如巴特所建议的,最好将AJAX调用改为get,因为它确实是从服务器获取一些东西,而不是发布一些东西。@Bartdude这是真的,我认为我解释得不好,但是如果你看到OP发布的示例,你可以看到他在试图做一个请愿书,我可以看到他在试图调用一个
Getdate()
方法。无论如何,你的答案是正确的,可以解决Op的问题,但是最好使用正确的HTTP方法,尤其是当你谈论get
&post
时。哦,太酷了!它成功了~我想我没有注意到“获取”部分,我正在试着做文章。但也要感谢大家的提醒@Bartdude谢谢大家~:)