Web Service not working in ASP.NET WebForms ajax call
I'm trying to call a simple web service like this, on the client side:
$.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 vo开发者_高级运维id 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:
The HTTP verb POST used to access path '/service/local/newsservice.asmx/DoPost' is not allowed.
When I use the plain URL, I get this instead (via Firebug, the application silently fails):
DoPost Web Service method name is not valid.
What could be going on?
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.
ServiceReference Class
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.
The proxy objects will be used to do the following:
Make asynchronous requests in JavaScript to Web service methods,
Initialize instances of proxies of server data types, in particular for use as input parameters for invoking Web methods.
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:
Exposing Web Services to Client Script
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.
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.
<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.
<system.web> <webServices> <protocols> <clear/> </protocols> </webServices> </system.web>
I think the problem is in using the
[ScriptMethod]
do you really need it here.
also check this may help:[ScriptMethod]
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
精彩评论