【发布时间】:2011-08-21 18:48:34
【问题描述】:
我正在尝试在客户端调用这样一个简单的 Web 服务:
$.ajax({
type: "POST",
url: "/service/local/newsservice.asmx/DoPost", // "/news/post/do",
data: {
title: _title,
markdown: _markdown,
categoryId: 1
},
success: function (data) {
alert("success!");
}
});
实际服务是:
[WebService(Namespace = "http://service.site.com/service/news")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
[ScriptService]
public class NewsService : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod]
public static void DoPost(string title, string markdown, int categoryId)
{
if (!(Roles.IsUserInRole("Owner") || Roles.IsUserInRole("Administrator")))
return;
CommunityNews.Post(title, markdown, categoryId);
}
}
当使用指向"/service/local/newsservice.asmx/DoPost" 的重写 URL 时,我收到以下错误:
用于访问路径的 HTTP 动词 POST '/service/local/newsservice.asmx/DoPost' 是不允许的。
当我使用纯 URL 时,我得到了这个(通过 Firebug,应用程序静默失败):
DoPost Web 服务方法名称无效。
会发生什么?
【问题讨论】:
-
在浏览器中调试和加载 Web 服务 URL 时会发生什么?您可以使用 ASP.NET 测试页发布数据吗?
-
您是否尝试过删除
[ScriptMethod]属性,使用[WebMethod]意味着接受POST请求,而当您想使用GET时,您会执行[ScriptMethod(UseHttpPost = true)]之类的操作
标签: jquery ajax web-services webforms