title: Ajax with jquery, scriptmanager and webservice which return null
body: <p>I dont get from my WebService data back and I dont know why. :/</p>
  
  <p>I have in my MasterPage.master this:</p>
  
  <pre><code>    &lt;asp:ScriptManager id="scriptMng" runat="server"&gt;
           &lt;Services&gt;
               &lt;asp:ServiceReference Path="~/WebServices/Mailing.asmx" /&gt;
           &lt;/Services&gt;
      &lt;/asp:ScriptManager&gt;
      &lt;asp:UpdatePanel runat="server" id="mailFormUpdatePanel" updateMode="Conditional"&gt;
           &lt;ContentTemplate&gt;
                /* form */
           &lt;/ContentTemplate&gt;
      &lt;/asp:UpdatePanel&gt;  
  </code></pre>
  
  <p>In my Mailing.asmx I have this:</p>
  
  <pre><code>&lt;%@ WebService Language="C#" CodeBehind="~/App_Code/Services/Mailing.cs" Class="Mailing" %&gt;
  </code></pre>
  
  <p>In my Mailing.asmx.cs I have this:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Services;
  using System.Net.Mail;
  using System.Text.RegularExpressions;
  
  /// &lt;summary&gt;
  /// Summary description for Mailing
  /// &lt;/summary&gt;
  [WebService(Namespace = "http://ktwebstudio.cz/WebServices/")]
  [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
  [System.Web.Script.Services.ScriptService]
  public class Mailing : System.Web.Services.WebService 
  {
  
      [WebMethod()]
      public string Mail(string fromName, string fromSurename, string fromEmail, string fromPhone, string phoneTimeFrom, string phoneTimeTo, string selectedJob, string mailMsg) 
      {
          if (fromName.Trim() != "" &amp;&amp; fromSurename.Trim() != "" &amp;&amp; fromName != "Jméno" &amp;&amp; fromSurename != "Příjmení" &amp;&amp; fromEmail.Trim() != "" &amp;&amp; fromEmail != "E-mailová adresa (povinné)")
          {
              bool error = false;
              if (fromPhone.Trim() != "" &amp;&amp; fromPhone != "Telefonní číslo (nepovinné)")
                  error = !IsNumber(fromPhone.Trim());
              if (IsWord(fromName.Trim()) &amp;&amp; IsWord(fromSurename.Trim()) &amp;&amp; IsEmail(fromEmail.Trim()) &amp;&amp; !error)
              {
                  /*
                  some logic
                  */
  
                  try { client.Send(mail); }
                  catch
                  {
                      return "Odesílání zprávy selhalo, zkuste prosím akci opakovat. &lt;br /&gt;Při přetrvávajících problémech zkuste použít alternativní způsob kontaktování, který naleznetev sekci &lt;a runat=\"server\" href=\"&lt;%$RouteUrl:RouteName=contact%&gt;\"&gt;kontakt&lt;/a&gt;.";
                  }
                  return "Zpráva byla úspěšně odeslána, děkujeme.";
              }
              else
                  return "Vaše jméno, příjmení nebo jeden z Vášich vyplněných kontaktů obsahuje neplatné znaky.";
          }
          else
              return "Musíte vyplnit Vaše jméno, příjmení a kontaktní email.";
      }
  
      public bool IsWord(string word)
      {
          Regex mask = new Regex(@"^[a-zA-Zá-žÁ-Ž]*$");
          return mask.IsMatch(word);
      }
      public bool IsEmail(string email)
      {
          Regex mask = new Regex(@"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$", RegexOptions.IgnoreCase);
          return mask.IsMatch(email);
      }
      public bool IsNumber(string number)
      {
          int num;
          return int.TryParse(number, out num);
      }
  }
  </code></pre>
  
  <p>So In my Masterpage.master.cs I test if browser support JS, if not i call web service from OnClick method where is this and i think that this was OK: (Dont ask me why I dont wanna have this if JS isnt supported)</p>
  
  <pre><code>Mailing mail = new Mailing();
  statusMsgLbl = mail.Mail(txtName.Text, txtSurename.Text, txtMail.Text, txtPhone.Text, startTime.SelectedValue, endTime.SelectedValue, selectJob.SelectedValue, txtMessage.Text);
  </code></pre>
  
  <p>But if JS is supported (i hope that user have it turn on) I have in my Masterpage.master this jQuery and javascript code:</p>
  
  <pre><code>    var msg;
      Sys.Application.add_load(load);
          function load() {
          $('#submit').click(function () {
             msg = Mailing.Mail($('#txtName').val(), $('#txtSurename').val(), $('#txtMail').val(), $('#startTime').val(), $('#endTime').val(), $('#selectJob').val(), $('#txtMessage').val());  
          });
      }
      Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(beginRequestHandler);
          function beginRequestHandler() {
              $(document.createElement('div'))
                          .attr('id', 'overlay')
                          .width($('#formBox').width())
                          .height($('#formBox').height())
                          .css({ backgroundImage: 'url(/Static/Img/bc_overlay.png)', position: 'absolute', left: 0, top: 0, margin: "5px", textAlign: "center", color: "#000", display: "none" })
                          .append("&lt;div id='loading' style='padding-top:100px'&gt;&lt;strong&gt;Odesílám&lt;/strong&gt;&lt;br /&gt;&lt;img src='Static/Img/ajax-loader.gif' width='33px' height='33px' alt='loading' /&gt;&lt;/div&gt;")
                          .show(500)
                          .prependTo($('#formBox'));
              $('#formBox').css('position', 'relative');
          }
      Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler);
      function endRequestHandler() {
         $('#loading').delay(2000).slideUp(800);
         $('#overlay').append("&lt;div style='padding-top:100px'&gt;&lt;strong id='statusMsg'&gt;&lt;/strong&gt;&lt;/div&gt;");
         if (msg != null)
            $('#statusMsg').css('display', 'none').delay(2800).fadeIn(300).text(msg);
         else
            $('#statusMsg').css('display', 'none').delay(2800).fadeIn(300).text("Problém");
         $('#overlay').delay(6000).hide(800);                                     
      }      
  </code></pre>
  
  <p><strong>First function use my WebService.</strong></p>
  
  <p><strong>Second function do some cool things when is beginRequest.</strong></p>
  
  <p><strong>And last function do some cool things too when is endRequest + write error / success message</strong>.. But i dont get my strings from Mailing.asmx.cs and I dont know why. :-/ I put into last function if(msg != null) ... So <strong>I know that msg is null</strong> and if u can tell me why, i will so grateful.</p>
  
  <p>(I tried it by $.ajax in jquery too, but i got 500 ERROR so I use this way with Sys.WebForms.PageRequestManager.)</p>