AJAX WebMethod 帮助
我一直在尝试从 jQuery AJAX 访问 .NET WebMethod,但无法让它工作。我已经阅读了在 SO 和其他网站上找到的所有内容,但似乎没有任何效果。
我的 C# 代码是这样的
[WebMethod]
public static string TestAjax()
{
return "Hello World";
}
,JavaScript 代码是
$.ajax({
type: "POST",
url: "ManageEvent.aspx/TestAjax",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg)
{
alert(msg.d);
},
error: function(result)
{
alert("error: " + result.status);
}
});
最终目标是使用 AJAX 向数据库提交表单,而无需重新加载整个页面,但目前我什至无法让它返回一个字符串。当我单击附加此代码的输入按钮时,错误代码被称为给出状态 12030。但是当我在 Visual Studio 之外测试代码时,我仍然出错,但状态为 200。我的代码中是否有任何明显的明显错误?会导致此错误的代码?
非常感谢
编辑:
通过安装 ASP.NET AJAX 1.0 并在我的 Javascript 中调用
PageMethods.TextAjax(OnSuccess, OnFail);
,其中 OnSuccess 和 OnFail 是函数。但是我仍然无法让 jQuery 的 AJAX 工作。它仍然抛出 12030 错误状态。
I have been trying to access a .NET WebMethod from jQuery AJAX but I cannot get it to work. I have read everything I can find on SO and other sites and nothing seems to be working.
My C# code is such
[WebMethod]
public static string TestAjax()
{
return "Hello World";
}
and the JavaScript code is
$.ajax({
type: "POST",
url: "ManageEvent.aspx/TestAjax",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg)
{
alert(msg.d);
},
error: function(result)
{
alert("error: " + result.status);
}
});
The end goal was to do a form submission to the database using AJAX without reloading the entire page but at the moment I can't even get it to return back a String. When I click the input button to which this code is attached the error code is called giving a status of 12030. But when I test the code outside of Visual Studio I still error but with a status of 200. Is there anything blatantly obvious in my code that would cause this error?
Thanks a ton
EDIT:
I have gotten the WebMethod to work using ASP.NET Ajax's ScriptManager by installing ASP.NET AJAX 1.0 and calling in my Javascript
PageMethods.TextAjax(OnSuccess, OnFail);
where OnSuccess and OnFail are functions. However I still cannot get jQuery's AJAX to work. It still throws the 12030 error status.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我看不出你的代码有什么问题。安装 FireBug 并查看您的设置问题出在哪里。这是一个完整的工作示例:
I can't see anything wrong with your code. Install FireBug and see where's the problem with your setup. Here's a full working example:
这一切看起来都是正确的。你的班级是不是这样:
That all looks correct. Does your class look like this:
Web 服务缺少 [ScriptService] 标记,该标记在安装 ASP.NET AJAX 后修复了该问题
The Web service was missing a [ScriptService] tag which fixed it after installing ASP.NET AJAX