Monday, June 29, 2009

[jQuery] Re: send parameters to web service method using jQuery .ajax function

                                    $.ajax({
                                        type: "POST", // use method POST
                                        url: "myservice.asmxx/getValue",
                                        data: "{'value': 'Hello World'}",
                                        contentType: "application/json; charset=utf-8",
                                        dataType: "json",
                                        success: function(data) {
                                             alert(data.d);
                                        },
                                        error: function() {
                                             alert("The was an error processing your request");
                                        }
                                    });


ensure this line is not commented out in your web service

[System.Web.Script.Services.ScriptService]

you can use the following web method to test

    [WebMethod]
    public string getValue(string value)
    {
        return "Your Value is " + value;
    }

On Wed, Jun 24, 2009 at 9:39 PM, nirtheking@gmail.com <nirtheking@gmail.com> wrote:

html code:
view plaincopy to clipboardprint?

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head runat="server">
  4.   <title></title>
  5.     <script type="text/javascript" src="jquery-1.2.3.min.js"></
script>
  6.   <script type="text/javascript">
  7.       /// <reference path="jquery-1.2.3.min.js" />
  8.       $(document).ready(function() {
  9.           $.ajax({
 10.               type: "POST",
 11.               url: "dummyWebservice.asmx/HelloToYou",
 12.               data: ({ name: <%= TextBox1.Text %> }),
 13.               contentType: "application/html; charset=utf-8",
 14.               dataType: "html",
 15.               success: function(msg) {
 16.                   $('#result').html(msg);
 17.               }
 18.           });
 19.       });
 20.   </script>
 21. </head>
 22. <body>
 23.   <form id="form1" runat="server">
 24.   <asp:TextBox ID="TextBox1" runat="server" Text="bla bla"></
asp:TextBox>
 25.   <div id="result"></div>
 26.   </form>
 27. </body>
 28. </html>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <title></title>
   <script type="text/javascript" src="jquery-1.2.3.min.js"></script>
 <script type="text/javascript">
     /// <reference path="jquery-1.2.3.min.js" />
     $(document).ready(function() {
         $.ajax({
             type: "POST",
             url: "dummyWebservice.asmx/HelloToYou",
             data: ({ name: <%= TextBox1.Text %> }),
             contentType: "application/html; charset=utf-8",
             dataType: "html",
             success: function(msg) {
                 $('#result').html(msg);
             }
         });
     });
 </script>
</head>
<body>
 <form id="form1" runat="server">
 <asp:TextBox ID="TextBox1" runat="server" Text="bla bla"></
asp:TextBox>
 <div id="result"></div>
 </form>
</body>
</html>


The dummyWebservice.cs file:
view plaincopy to clipboardprint?

  1. [WebService(Namespace = "http://tempuri.org/")]
  2.  [WebServiceBinding(ConformsTo =
WsiProfiles.BasicProfile1_1)]
  3.  [System.Web.Script.Services.ScriptService]
  4.  public class dummyWebservice :
System.Web.Services.WebService
  5.  {
  6.    [WebMethod()]
  7.     public string HelloToYou(string name)
  8.    {
  9.         return "Hello " + name;
 10.    }
 11. }

[WebService(Namespace = "http://tempuri.org/")]
 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
 [System.Web.Script.Services.ScriptService]
 public class dummyWebservice : System.Web.Services.WebService
 {
  [WebMethod()]
   public string HelloToYou(string name)
  {
       return "Hello " + name;
  }
}



I want - when the page load in result div will apper "Hello bla bla".

It works for me if I dont use parameter(aka  data: "{ }" and
HelloToYou dont accept parameters)

No comments: