I'm trying to call a simple web service like this, on the client side:
我试图在客户端调用这样一个简单的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!");
}
});
The actual service is:
实际服务是:
[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);
}
}
When using the rewritten URL, which points to "/service/local/newsservice.asmx/DoPost"
, I get the following error:
使用指向“/service/local/newsservice.asmx/DoPost”的重写URL时,出现以下错误:
The HTTP verb POST used to access path '/service/local/newsservice.asmx/DoPost' is not allowed.
用于访问路径'/service/local/newsservice.asmx/DoPost'的HTTP谓词POST是不允许的。
When I use the plain URL, I get this instead (via Firebug, the application silently fails):
当我使用普通URL时,我得到了这个(通过Firebug,应用程序静默失败):
DoPost Web Service method name is not valid.
DoPost Web服务方法名称无效。
What could be going on?
会发生什么事?
2 个解决方案
#1
1
The built-in way of calling a web service in ASP.NET is to use a service reference, which creates JavaScript objects for you to call your web service methods.
在ASP.NET中调用Web服务的内置方法是使用服务引用,该引用为您创建JavaScript对象以调用Web服务方法。
ServiceReference类
To call Web service methods from ECMAScript (JavaScript), you must include a service reference in the ASP.NET page and apply the ScriptServiceAttribute attribute to the Web service class definition. If you include a service reference to a Web service in the ScriptManager or ScriptManagerProxy control inside the ASP.NET page, JavaScript objects will be instantiated in the browser.
要从ECMAScript(JavaScript)调用Web服务方法,必须在ASP.NET页面中包含服务引用,并将ScriptServiceAttribute属性应用于Web服务类定义。如果在ASP.NET页面内的ScriptManager或ScriptManagerProxy控件中包含对Web服务的服务引用,则将在浏览器中实例化JavaScript对象。
The proxy objects will be used to do the following:
代理对象将用于执行以下操作:
Make asynchronous requests in JavaScript to Web service methods,
在JavaScript中向Web服务方法发出异步请求,
Initialize instances of proxies of server data types, in particular for use as input parameters for invoking Web methods.
初始化服务器数据类型的代理实例,特别是用作调用Web方法的输入参数。
Since you're using jQuery instead of the proxy objects created for ASP.NET AJAX, you might have to check a couple things are configured properly:
由于您使用的是jQuery而不是为ASP.NET AJAX创建的代理对象,因此您可能需要检查一些正确配置的内容:
Exposing Web Services to Client Script
将Web服务公开给客户端脚本
To enable Web service calls from [ASP.NET AJAX] script, you must register the ScriptHandlerFactory HTTP handler in the application's Web.config file. The handler processes calls made from script to .asmx Web services. The following example shows the Web.config element for adding the handler.
要从[ASP.NET AJAX]脚本启用Web服务调用,必须在应用程序的Web.config文件中注册ScriptHandlerFactory HTTP处理程序。处理程序处理从脚本到.asmx Web服务的调用。以下示例显示了用于添加处理程序的Web.config元素。
These configuration settings are already part of the Web.config file template for any new AJAX-enabled Web sites that you create in Microsoft Visual Studio 2005.
这些配置设置已经是您在Microsoft Visual Studio 2005中创建的任何新的启用AJAX的网站的Web.config文件模板的一部分。
<system.web> <httpHandlers> <remove verb="*" path="*.asmx"/> <add verb="*" path="*.asmx" type="System.Web.Script.Services.ScriptHandlerFactory" validate="false"/> </httpHandlers> <system.web>
For Web service calls that are not issued from ASP.NET AJAX script, the ScriptHandlerFactory handler delegates the call to the default handler, which uses SOAP instead of JSON format. The delegation is performed automatically and you do not have to take any action unless you want to disable the use of the SOAP protocol for the Web services. In that case, you must enter the following configuration setting in the Web.config file.
对于非ASP.NET AJAX脚本发出的Web服务调用,ScriptHandlerFactory处理程序将调用委托给默认处理程序,该处理程序使用SOAP而不是JSON格式。委派是自动执行的,除非您要禁用Web协议的SOAP协议,否则不必执行任何操作。在这种情况下,您必须在Web.config文件中输入以下配置设置。
<system.web> <webServices> <protocols> <clear/> </protocols> </webServices> </system.web>
#2
0
I think the problem is in using the
我认为问题在于使用
[ScriptMethod]
[ScriptMethod]
do you really need it here.
你真的需要它吗?
also check this may help:[ScriptMethod]
还检查这可能会有所帮助:[ScriptMethod]
http://msdn.microsoft.com/en-us/library/system.web.script.services.scriptmethodattribute.aspx
http://msdn.microsoft.com/en-us/library/system.web.script.services.scriptmethodattribute.aspx
http://msdn.microsoft.com/en-us/library/byxd99hx(v=vs.71).aspx
http://msdn.microsoft.com/en-us/library/byxd99hx(v=vs.71).aspx
#1
1
The built-in way of calling a web service in ASP.NET is to use a service reference, which creates JavaScript objects for you to call your web service methods.
在ASP.NET中调用Web服务的内置方法是使用服务引用,该引用为您创建JavaScript对象以调用Web服务方法。
ServiceReference类
To call Web service methods from ECMAScript (JavaScript), you must include a service reference in the ASP.NET page and apply the ScriptServiceAttribute attribute to the Web service class definition. If you include a service reference to a Web service in the ScriptManager or ScriptManagerProxy control inside the ASP.NET page, JavaScript objects will be instantiated in the browser.
要从ECMAScript(JavaScript)调用Web服务方法,必须在ASP.NET页面中包含服务引用,并将ScriptServiceAttribute属性应用于Web服务类定义。如果在ASP.NET页面内的ScriptManager或ScriptManagerProxy控件中包含对Web服务的服务引用,则将在浏览器中实例化JavaScript对象。
The proxy objects will be used to do the following:
代理对象将用于执行以下操作:
Make asynchronous requests in JavaScript to Web service methods,
在JavaScript中向Web服务方法发出异步请求,
Initialize instances of proxies of server data types, in particular for use as input parameters for invoking Web methods.
初始化服务器数据类型的代理实例,特别是用作调用Web方法的输入参数。
Since you're using jQuery instead of the proxy objects created for ASP.NET AJAX, you might have to check a couple things are configured properly:
由于您使用的是jQuery而不是为ASP.NET AJAX创建的代理对象,因此您可能需要检查一些正确配置的内容:
Exposing Web Services to Client Script
将Web服务公开给客户端脚本
To enable Web service calls from [ASP.NET AJAX] script, you must register the ScriptHandlerFactory HTTP handler in the application's Web.config file. The handler processes calls made from script to .asmx Web services. The following example shows the Web.config element for adding the handler.
要从[ASP.NET AJAX]脚本启用Web服务调用,必须在应用程序的Web.config文件中注册ScriptHandlerFactory HTTP处理程序。处理程序处理从脚本到.asmx Web服务的调用。以下示例显示了用于添加处理程序的Web.config元素。
These configuration settings are already part of the Web.config file template for any new AJAX-enabled Web sites that you create in Microsoft Visual Studio 2005.
这些配置设置已经是您在Microsoft Visual Studio 2005中创建的任何新的启用AJAX的网站的Web.config文件模板的一部分。
<system.web> <httpHandlers> <remove verb="*" path="*.asmx"/> <add verb="*" path="*.asmx" type="System.Web.Script.Services.ScriptHandlerFactory" validate="false"/> </httpHandlers> <system.web>
For Web service calls that are not issued from ASP.NET AJAX script, the ScriptHandlerFactory handler delegates the call to the default handler, which uses SOAP instead of JSON format. The delegation is performed automatically and you do not have to take any action unless you want to disable the use of the SOAP protocol for the Web services. In that case, you must enter the following configuration setting in the Web.config file.
对于非ASP.NET AJAX脚本发出的Web服务调用,ScriptHandlerFactory处理程序将调用委托给默认处理程序,该处理程序使用SOAP而不是JSON格式。委派是自动执行的,除非您要禁用Web协议的SOAP协议,否则不必执行任何操作。在这种情况下,您必须在Web.config文件中输入以下配置设置。
<system.web> <webServices> <protocols> <clear/> </protocols> </webServices> </system.web>
#2
0
I think the problem is in using the
我认为问题在于使用
[ScriptMethod]
[ScriptMethod]
do you really need it here.
你真的需要它吗?
also check this may help:[ScriptMethod]
还检查这可能会有所帮助:[ScriptMethod]
http://msdn.microsoft.com/en-us/library/system.web.script.services.scriptmethodattribute.aspx
http://msdn.microsoft.com/en-us/library/system.web.script.services.scriptmethodattribute.aspx
http://msdn.microsoft.com/en-us/library/byxd99hx(v=vs.71).aspx
http://msdn.microsoft.com/en-us/library/byxd99hx(v=vs.71).aspx