2015-04-01 57 views
0

我從我的ASP.Net Web應用程序使用jquery調用WCF服務來測試我的WCF服務。正在調用服務函數GetData,但參數值爲空。我無法將數據傳遞給WCF服務。 我已經嘗試了不同的選項來調整內容類型,但沒有成功。JSON數據沒有從JQuery傳遞到使用AJAX的ASP.Net MVC

WCF服務代碼如下

namespace MapsSvc 
{ 
    //[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
[ServiceBehavior(IncludeExceptionDetailInFaults = true)] 
public class MapService : IMapService 
{ 

    //[OperationContract] 
    [WebInvoke(Method = "POST", 
        BodyStyle = WebMessageBodyStyle.Wrapped, 
        ResponseFormat = WebMessageFormat.Json, 
        RequestFormat = WebMessageFormat.Json)] 
    public string GetData(string value) 
    { 
     return string.Format("You entered: {0}", value); 
    } 
} 
} 

接口文件代碼如下

namespace MapsSvc 
{ 
[ServiceContract] 
public interface IMapService 
{ 

    [OperationContract] 
    string GetData(string value); 

} 
} 

web.config文件設置如下

 <?xml version="1.0"?> 
<configuration> 
    <appSettings> 
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> 
    </appSettings> 
    <system.web> 
    <compilation debug="true" targetFramework="4.5" /> 
    <httpRuntime targetFramework="4.5"/> 
    <authentication mode="Windows" /> 
    </system.web> 
    <system.serviceModel> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="ServiceBehavior"> 
      <!-- To avoid disclosing metadata information, set the values below to false before deployment --> 
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/> 
      <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> 
      <serviceDebug includeExceptionDetailInFaults="true"/> 
     </behavior> 
     </serviceBehaviors> 
     <endpointBehaviors> 
     <behavior name="EndpBehavior"> 
      <webHttp /> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    <services> 
     <!--<service name="AJAXEnabledWebService.Service"> 
     <endpoint address="" behaviorConfiguration="AJAXEnabledWebService.ServiceAspNetAjaxBehavior" 
      binding="webHttpBinding" contract="AJAXEnabledWebService.IService" /> 
     </service>--> 
     <service behaviorConfiguration="ServiceBehavior" name="MapsSvc.MapService"> 
     <endpoint address="" binding="webHttpBinding" contract="MapsSvc.IMapService" behaviorConfiguration="EndpBehavior"/> 
     </service> 
    </services> 
    <protocolMapping> 
     <add binding="basicHttpsBinding" scheme="https" /> 
    </protocolMapping>  
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    <directoryBrowse enabled="true"/> 
    </system.webServer> 

</configuration> 

在HTML發送JSON數據到WCF服務的文件如下

 <!DOCTYPE html> 
<html> 
<head> 
    <title>Call WCF</title> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> 
    <script type="text/javascript"> 

     var counter = 0; 
     $.support.cors = true; 
     function CallMyService() { 
      counter++; 
      counter = "Nate"; 
      $.ajax({ 
       type: "POST", 
       url: "http://localhost:63739/MapService.svc/GetData", 
       data: '{"Count": "' + counter + '"}', 
       contentType: "application/json", 

       success: ServiceSucceeded, 
       error: ServiceFailed 
      }); 
     } 

     // ---- WCF Service call backs ------------------- 

     function ServiceFailed(result) { 
      Log('Service call failed: ' + result.status + ' ' + result.statusText); 
     } 

     function ServiceSucceeded(result) { 
      var resultObject = result.MyFunctionResult; 
      Log("Success: " + resultObject); 
     } 

     // ---- Log ---------------------------------------- 
     function Log(msg) { 
      $("#logdiv").append(msg + "<br />"); 
     } 
    </script> 
</head> 
<body> 
    <input id="Button1" type="button" value="Execute" onclick="CallMyService();" /> 

    <div id="logdiv"></div> <!--For messages--> 
</body> 
</html> 
+1

你確定你在ajax調用中發送正確的值嗎?在我看來,你提供{「Count」:「1」},而不是{「value」:「1」} – 2015-04-01 21:37:32

+0

你是對的,當我改變它的值時,它會讓我看到價值。謝謝。 – Nate 2015-04-01 21:49:56

+0

不客氣:) – 2015-04-01 21:52:24

回答

0
.ajax({ 
      type: "POST", 
      url: "http://localhost:63739/MapService.svc/GetData", 
      data: '{"value": "' + counter + '"}', 
      contentType: "application/json", 

它應該是有價值的。謝謝盧卡斯

相關問題