2017-02-18 28 views
0

的获取工作正常,但通过使用ID或POST的get PUT将返回错误404WCF作为html页面的数据服务。 jQuery的Ajax功能。

WCF DataService.svc

公共功能GetHealthcare(BYVAL类别ID作为字符串)作为vwHealthcare器具IDataService。 GetHealthcare 昏暗nSubject作为新vwHealthcare

Dim context As DataClasses1DataContext = New DataClasses1DataContext() 
    'Check if exists 
    Dim res = From b In context.vwHealthcares Where b.CategoryId = CategoryId 
       Select b 
    Dim uList As List(Of vwHealthcare) = res.ToList() 

    Return res 
End Function 

IDataService

<WebInvoke(Method:="POST", ResponseFormat:=WebMessageFormat.Json, UriTemplate:="GetHealthcareByCategoryId/{CategoryId}")> 
<OperationContract()> 
Function GetHealthcare(ByVal CategoryId As String) As vwHealthcare 

我的客户端HTML:

 




     // Urls to access the WCF Rest service methods 


     var urlsource ; 

     urlsource = "http://localhost/ehealthservice/DataService.svc/"; 
     var varType; 
     var varUrl; 
     var method; 
     var varData; 
     var varContentType; 
     var varDataType; 
     var varProcessData; 

     //Generic function to call AXMX/WCF Service 
     function CallServiceC() { 
      debugger; 
      var categoryid = getQueryStringValue('CategoryId'); 

      var oData; 

      debugger; 
      $.ajax({ 
       cache: false, 
       type: varType, //GET or POST or PUT or DELETE verb 
       url: varUrl, // Location of the service 
       data: oData, //Data sent to server 

       contentType: varContentType, // content type sent to server 
       crossDomain: true, 
       dataType: varDataType, 
       timeout: 2000, 

       success: function (msg) {//On Successfull service call 

        debugger; 
        // I want to get here 





          return html; 

         } 

       }, 
       error: ServiceFailedC// When Service call fails 
      }); 


     } 


     function ServiceFailedC(result) { 
      debugger; 
      var myJSON = JSON.stringify(result); 

      alert('Service call failed: ' + result.status + '' + result.statusText); 
      varType = null; varUrl = null; varData = null; varContentType = null; varDataType = null; varProcessData = null; 
     } 


     function GetHealthcareByCategory() { 
      debugger; 

      varType = "POST"; 
      varUrl = urlsource + "GetHealthcareByCategoryId/1"; 

      varContentType = "application/json; charset=utf-8"; 
      varDataType = "jsonp"; 
      varProcessData = false; 
      method = "GetHealthcare"; 

      CallServiceC(); 
     } 

     function getQueryStringValue(key) { 
      debugger; 
      return unescape(window.location.search.replace(new RegExp("^(?:.*[&\\?]" + escape(key).replace(/[\.\+\*]/g, "\\$&") + "(?:\\=([^&]*))?)?.*$", "i"), "$1")); 
     }; 
     $(document).ready(

    function() { 




     GetHealthcareByCategory(); 
    } 

    ); 
    

注: 我已经尝试了许多方法还是一样的404错误。我也启用了文件夹的读/写权限。本示例遵循Pranya示例,链接enter link description here,并且我仍然收到错误404请任何开发人员在房子里。

回答

0

经过几天的搜索和尝试后发现它。必须通过在Global.asax的begin_Request中添加交叉源资源共享(CORS)并清理Web配置。解决方案的链接在这里Enable CORS In WCF

注意:我通过使用jsonp获取了它,但POST,PUT和DELETE无法执行。 非常感谢。