Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 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:

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?

share|improve this question
 
What happens when you debug and load your web service URL in a browser? Can you post data with the ASP.NET test pages? –  nekno Aug 21 '11 at 18:58
 
have you tried removing the [ScriptMethod] attribute, using [WebMethod] means accepting the POST requests where as when you want to use GET you do something like [ScriptMethod(UseHttpPost = true)] –  3nigma Aug 21 '11 at 19:47
 
Yes, I have tried that –  Nico Aug 21 '11 at 19:53
add comment

2 Answers

up vote 0 down vote accepted

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>
share|improve this answer
1  
Following this post, I created a new service and tested from that one, it worked, a few attempts to make them similar later, I realized my method was static, because I had copied and pasted it from the web page it originally was in. –  Nico Aug 21 '11 at 21:19
 
@Nico - That's a good point, and it makes sense! Web page methods decorated with the PageMethod attribute need to be static to be called from script, but web service methods should be ordinary public methods. –  nekno Aug 21 '11 at 21:52
add comment

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

share|improve this answer
 
Still the same errors. –  Nico Aug 21 '11 at 19:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.