无法从jquery / Ajax调用WCF服务

时间:2014-08-21 17:56:08

标签: c# wcf

我创建了一个宁静的wcf服务。当我通过URL调用时,我可以看到数据,但是当我通过客户端调用它时,它将进入错误方法。这是我到目前为止编码的内容。

这是界面:

public interface IService1
{

    [OperationContract]
    [WebGet(ResponseFormat=WebMessageFormat.Json)] 
    string GetPlayers();
}

[DataContract]
public class UserDetails
{
    string userid = string.Empty;
    string username = string.Empty;
    string location = string.Empty;
    [DataMember]
    public string UserId { get; set; }
    [DataMember]
    public string UserName { get; set; }
    [DataMember]
    public string Role { get; set; }
}

这是服务类

public class Service1 : IService1
{

    public string GetPlayers()
    {
        List<UserDetails> UserInfo = new List<UserDetails>();
        UserDetails ud = new UserDetails();
        ud.UserName = "RajSingh";
        ud.UserId = "MyID";
        ud.Role = "SU";
        UserInfo.Add(ud);
        JavaScriptSerializer jss = new JavaScriptSerializer();
        return (jss.Serialize(UserInfo));
    }
}

的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"/>
  </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"/>
          <!-- 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="ServiceAspNetAjaxBehavior" >
                <webHttp/>
            </behavior>
        </endpointBehaviors>
    </behaviors>
      <services>
          <service behaviorConfiguration="ServiceBehavior" name="TestJSONService.Service1" >
                  <endpoint address="" binding="webHttpBinding" contract="TestJSONService.IService1" behaviorConfiguration="ServiceAspNetAjaxBehavior">

                  </endpoint>
                    <host>
              <baseAddresses>
                <add baseAddress="http://localhost/TestJSONService"/>
              </baseAddresses>
            </host>
                  <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
            </service>
      </services>

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

客户端ASPX页面

<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>

    <script type="text/javascript">

        $(function () {
            $.ajax({
                type: "GET",
                contentType: "application/json; charset=utf-8",
                url: 'http://localhost/TestJSONService/Service1.svc/GetPlayers',
                datatype: "json",
                processData: false,
                success: function (data) {
                    //alert(data.d);
                    $($.parseJSON(data.d)).each(function (index, value) {
                        $("#TableID").append("<tr><td>" + value.UserName + "</td><td>" + value.UserID + "</td><td>" + value.Role + "</td></tr>");
                    });
                },
                error: function (result) {
                    alert('error');
                }
            });

        });

</script>

当我通过URL

调用服务时,我可以看到JSON格式的数据

但是当我通过客户端应用程序调用它时,它将进入错误块。

知道我做错了什么。

任何帮助都将非常感激

很抱歉,由于我没有足够的声誉,我无法发布图片。

0 个答案:

没有答案