简体   繁体   中英

Unable to call WCF Restful service from jQuery

Following is my code from jQuery script:

    $(document).ready(function () {
          $.ajax({
              cache: false,
              type: "GET",
              async: true,                  
              dataType: "json",
              url: "http://localhost:9002/SampleServices/Service/REST/Employees",                  
              processdata: false,                  
              error: ServiceFailed,
              success: function () { alert("succeeded");   }
          });
      });

      function ServiceFailed(result) {
          alert('Service call failed: ' + result.status + '' + result.statusText);
          Type = null;
          Url = null;
          Data = null;
          ContentType = null;
          DataType = null;
          ProcessData = null;
      }

      function ServiceFailed(xhr) {
          alert("error");
          alert(xhr.responseText);
          if (xhr.responseText) {
              var err = xhr.responseText;
              if (err)
                  error(err);
              else
                  error({ Message: "Unknown server error." })
          }
          return;
      }

When I run the above code it always enters ServiceFailed(xhr) and alerts "error".

When I try to execute the above url ("http://localhost:9002/SampleServices/Service/REST/Employees") through the browser I get following JSON response: {"page":1,"records":1,"rows":[{"cell":["1","Haris","21-03-1979","HR"],"id":1}],"total":1}

Fiddler says following when I run the jQuery script:

#   Result  Protocol    Host    URL Body    Caching Content-Type    Process Comments    Custom  
1   200 HTTP    localhost:9002  /SampleServices/Service/REST/Employees?_=1340833203960  89      application/json; charset=utf-8 chrome:1448         

carlosfigueira nailed it.

This was a cross domain request hence i needed to add crossdomain support in App.config file for my WCF service

From the documentation , the error callback function accepts three parameters: jqXHR , textStatus , and errorThrown .

Your method only has one parameter. You should start by having your error method take three parameters and inspect what values they have.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM