ASP .NET:无法使用 jQuery 调用 Page WebMethod
我在页面的代码隐藏文件中创建了一个 WebMethod,如下所示:
[System.Web.Services.WebMethod()]
public static string Test()
{
return "TEST";
}
我创建了以下 HTML 页面来测试它:
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"/></script>
<script type="text/javascript">
function test() {
$.ajax({
type: "POST",
url: "http://localhost/TestApp/TestPage.aspx/Test",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "text",
success: function(msg) {
alert(msg.d);
}
});
}
</script>
</head>
<body>
<button onclick="test();">Click Me</button>
</body>
</html>
当我单击按钮时,AJAX 会触发,但不会返回任何内容。当我调试代码时,Test()
方法甚至不会被调用。有什么想法吗?
I created a WebMethod in the code-behind file of my page as such:
[System.Web.Services.WebMethod()]
public static string Test()
{
return "TEST";
}
I created the following HTML page to test it out:
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"/></script>
<script type="text/javascript">
function test() {
$.ajax({
type: "POST",
url: "http://localhost/TestApp/TestPage.aspx/Test",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "text",
success: function(msg) {
alert(msg.d);
}
});
}
</script>
</head>
<body>
<button onclick="test();">Click Me</button>
</body>
</html>
When I click the button, the AJAX fires off, but nothing is returned. When I debug my code, the method Test()
doesn't even get called. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试
或任何将访问您的页面的相对网址。
您可能无意中违反了同源政策。
另外,尽管您还没有到达那里,但您正在期待 ad:wrapped 对象。事实上,你只会得到一根绳子。
这应该可以带你去你想去的地方。
try
or whatever relative url that will hit your page.
You may be inadvertently violating same origin policy.
Also, although you are not there yet, you are expecting a d: wrapped object. As it is you are just going to get a string.
This should get you where you want to go.
我认为数据类型应该是“json”。添加一个错误函数来查看返回的错误状态,即 404 未找到、500 服务器错误等
I think datatype should be "json". Add an error function to see what error status you get back ie 404 not found , 500 server error etc etc
我制作了这个 javascript 函数来使用 jQuery 调用 WebMethods:
$.toJson 序列化是通过 jquery.json-1.3 插件实现的。
正如你所看到的,dataType 必须是“json”
I made this javascript function to call WebMethods using jQuery:
That $.toJson serialization is realized by jquery.json-1.3 plugin.
And as you can see, dataType must be "json"
您需要将 Test() 设置为接受/允许 POST
you need to set Test() to accept/allow POST
如果 PageMethods 在您的页面上正确注册,您应该能够使用 Microsoft 注册的名为 PageMethods 的对象来调用它们。
您的 javascript 应在 aspx 页面加载所有 Microsoft 特定库后运行。加载这些内容后,您可以通过以下方式调用 PageMethod:
PageMethods.Test(function() OnSucceeded{}, function() OnFailed{});
以下是更好示例的链接:
http://www.junasoftware.com/blog/using-jquery-ajax-and-page-methods-with-a-asp.net-webservice.aspx
如果您不是我已经强烈建议使用 Firebug 来帮助调试这些客户端调用。 Firebug 将为您提供确定实际情况所需的所有信息。
getfirebug.com
If the PageMethods are properly registered on your page, you should be able to call them with a Microsoft registered object called PageMethods.
Your javascript should run after the aspx page has loaded all the Microsoft specific libraries. When those are loaded, you could call your PageMethod this way:
PageMethods.Test(function() OnSucceeded{}, function() OnFailed{});
Here is a link to better examples:
http://www.junasoftware.com/blog/using-jquery-ajax-and-page-methods-with-a-asp.net-webservice.aspx
If you aren't already, I highly recommend using Firebug to help debug these client-side calls. Firebug will give you all the info you need to determine what is really going on.
getfirebug.com