views:

349

answers:

2

I am making an AJAX call in my ASP.NET application via Jquery to a page method.

   $.ajax({
       type: "POST",
       url: "APage.aspx/GetDropDowns",
       data: "{'AId':'1'}",
       contentType: "application/json; charset=utf-8",
       dataType: "json",
       success: function(msg) {
         alert(msg.d);
       },
       error: function() {
         alert('Error ');
       }

    [System.Web.Services.WebMethod]
public static string GetDropDowns(string Id)
{
  return "Id was: " + Id;
}

I remember from using ASP.NET AJAX that the use of webservices was encouraged as opposed to page methods. However if I am enforcing JSON as above and doing a post is there A) any security flaw with the above and B) any reason to use a webservice rather than the page method

A: 

A. Here's a good reply already in SO (http://stackoverflow.com/questions/38421/security-advice-for-jquery-ajax-data-post)

B. The page postback method (or via Ajax UpdatePanel) has bigger overhead at the HTTP Post (especially the viewstate information, which carries a lot more data that are not required). Normal postback is more straightforward to implement though.

o.k.w
I don't believe the question was page postback vs updatepanel, rather he's asking about Web Service vs. Page Method.
Herb Caudill
A: 

A web method is essentially just a simple way of implementing a web service without creating a separate file etc. for it. There's nothing wrong with the code you've posted. @John's answer on the question 38421 referenced by @o.k.w. is a good summary of what you need to think about in terms of security.

Herb Caudill