JSON数据没有使用AJAX从JQuery传递到ASP.Net MVC

时间:2022-10-24 21:34:08

I am calling a WCF Service from my ASP.Net web application using a jquery to test my WCF service. The service function GetData is being called, but the parameter value is null. I am not able to pass the data to the WCF service. I have tried different options with respect to adjusting content types, with no success.

我使用jquery从我的ASP.Net Web应用程序调用WCF服务来测试我的WCF服务。正在调用服务函数GetData,但参数值为null。我无法将数据传递给WCF服务。我在调整内容类型方面尝试了不同的选项,没有成功。

The WCF service code is as follows

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);
    }
}
}

The interface file code is as follows

接口文件代码如下

namespace MapsSvc
{
[ServiceContract]
public interface IMapService
{

    [OperationContract]
    string GetData(string value);

}
}

The web.config file setting are as follows

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>

The html file which sends JSON data to WCF Service is as follows

将JSON数据发送到WCF服务的html文件如下所示

      <!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 个解决方案

#1


0  

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

It should have been value. Thank you Lukas

它应该是有价值的。谢谢Lukas

#1


0  

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

It should have been value. Thank you Lukas

它应该是有价值的。谢谢Lukas