title: Calling WCF with Ajax - 404 Error
body: <p>I've been trying to follow this <a href="http://www.encodedna.com/wcf/tutorial/invoke-a-wcf-service-method-with-multiple-parameters.htm" rel="nofollow">article</a> and create a WCF Service to access with an Ajax call. I am getting a 404 on my Ajax call and have no idea why because it looks ok to me. Can someone please help me fix the 404 error and show me where I have a namespace or whatever wrong? </p>
  
  <p>Before anyone yells at me, I have never created a WCF service (let alone call it from Ajax) so I am following the article, which says do not worry about an interface right now :)</p>
  
  <pre><code>namespace SearchService
  {
      [ServiceContract]
      [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
      public class Search
      {
          [OperationContract]
          [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
          public string SearchFiles(string searchText)
          {
              StringBuilder result = new StringBuilder();
  
              try
              {
                  string currentFolder = Path.GetDirectoryName(@"C:\somefolder\views\filteredViews\");
  
                  string[] files = Directory.GetFiles(currentFolder, "*.htm");
  
                  foreach (string file in files)
                  {
                      string contents = File.ReadAllText(file);
  
                      if (contents.Contains(searchText))
                      {
                          result.AppendLine(file);
                          result.Append("|");
                      }
                  }
              }
              catch (Exception ex)
              {
                  Console.WriteLine(ex.Message);
              }
  
              return result.ToString();
          }
      }
  }
  
  &lt;system.serviceModel&gt;
      &lt;behaviors&gt;
          &lt;serviceBehaviors&gt;
              &lt;behavior name="SearchServiceBehavior"&gt;
                  &lt;serviceMetadata httpGetEnabled="true" /&gt;
                  &lt;serviceDebug includeExceptionDetailInFaults="true"/&gt;
              &lt;/behavior&gt;
          &lt;/serviceBehaviors&gt;
          &lt;endpointBehaviors&gt;
              &lt;behavior name="webEndPointBehavior"&gt;
                  &lt;webHttp/&gt;
              &lt;/behavior&gt;
          &lt;/endpointBehaviors&gt;
      &lt;/behaviors&gt;
      &lt;protocolMapping&gt;
          &lt;add binding="basicHttpsBinding" scheme="https" /&gt;
      &lt;/protocolMapping&gt;
      &lt;serviceHostingEnvironment aspNetCompatibilityEnabled="true" 
                                 multipleSiteBindingsEnabled="true" /&gt;
      &lt;services&gt;
          &lt;service name="SearchService.Search" behaviorConfiguration="SearchServiceBehavior"&gt;
              &lt;endpoint address="" 
                        contract="SearchService.Search" 
                        binding="webHttpBinding" 
                        behaviorConfiguration="webEndPointBehavior" 
                        name="webEndPoint" /&gt;
          &lt;/service&gt;
      &lt;/services&gt;
  &lt;/system.serviceModel&gt;
  
  var Type;
  var Url;
  var Data;
  var ContentType;
  var DataType;
  var ProcessData;
  
  $("#searchButton").click(function () {
      var searchText = $("#searchText").val();
      Type = "POST";
      Url = "Search.svc/SearchFiles";
      Data = '{"' + searchText + '"}';
      ContentType = "application/json; charset=utf-8";
      DataType = "json";
      varProcessData = true;
      CallService();
  });
  
  // Function to call WCF  Service       
  function CallService() {
      $.ajax({
          type: Type,                 //GET or POST or PUT or DELETE verb
          url: Url,                   // Location of the service
          data: Data,                 //Data sent to server
          contentType: ContentType,   // content type sent to server
          dataType: DataType,         //Expected data format from server
          processdata: ProcessData,   //True or False
          success: function(msg) {    //On Successfull service call
              ServiceSucceeded(msg);
          },
          error: ServiceFailed        // When Service call fails
      });
  }
  
  function ServiceFailed(result) {
      alert('Service call failed: ' + result.status + '' + result.statusText);
      Type = null;
      varUrl = null;
      Data = null; 
      ContentType = null;
      DataType = null;
      ProcessData = null;
  }
  
  function ServiceSucceeded(result) {
      if (DataType == "json") {
          alert("It worked" + result);
      }
  }
  
  function ServiceFailed(xhr) {
      if (xhr.responseText) {
          var err = xhr.responseText;
          if (err)
              error(err);
          else
              error({ Message: "Unknown server error." })
      }
  
      return;
  }
  </code></pre>