简体   繁体   English

无法从jQuery调用WCF Restful服务

[英]Unable to call WCF Restful service from jQuery

Following is my code from jQuery script: 以下是我来自jQuery脚本的代码:

    $(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". 当我运行上面的代码时,它总是输入ServiceFailed(xhr)并警告“错误”。

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} 当我尝试通过浏览器执行上述url(“ http:// localhost:9002 / SampleServices / Service / REST / Employees”)时,我得到以下JSON响应:{“ 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: 当我运行jQuery脚本时,Fiddler表示以下内容:

#   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. carlosfigueira钉了它。

This was a cross domain request hence i needed to add crossdomain support in App.config file for my WCF service 这是一个跨域请求,因此我需要为WCF服务在App.config文件中添加跨域支持

From the documentation , the error callback function accepts three parameters: jqXHR , textStatus , and errorThrown . 文档中 ,错误回调函数接受三个参数: jqXHRtextStatuserrorThrown

Your method only has one parameter. 您的方法只有一个参数。 You should start by having your error method take three parameters and inspect what values they have. 您应该首先让错误方法采用三个参数并检查它们具有哪些值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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