2015-04-01 56 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", 

它应该是有价值的。谢谢卢卡斯

相关问题