WCF:服务调用失败:415不支持的媒体类型

时间:2013-07-10 10:43:23

标签: asp.net ajax wcf jquery

我正在使用asp.net 3.5。我开发了WCF服务。我尝试调用WCF服务表单JQUERY,但我收到错误

服务呼叫失败:415不支持的媒体类型

我的守则如下: -

IService: -

[ServiceContract]
public interface IService
{
    [OperationContract]
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json)]
    CommandStatus getCommandStatus(string Id, string UnitNumber, string UnitType);
}

服务: -

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class Service : IService
{
    public CommandStatus getCommandStatus(string Id, string UnitNumber, string UnitType)
    {
        CommandStatus objCommandStatus = new CommandStatus();
        CommandsBLL objCommandsBLL = new CommandsBLL();
        DataSet ds = new DataSet();
        ds = objCommandsBLL.getCommandStatus(Convert.ToInt32(Id), UnitNumber, Convert.ToInt32(UnitType));

        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            objCommandStatus.ID = Convert.ToString(ds.Tables[0].Rows[0]["IOMH_ID"]);
            objCommandStatus.UnitType = Convert.ToString(ds.Tables[0].Rows[0]["IOMH_UnitType"]);
        }

        return objCommandStatus;
    }
}

Web.Config文件是

<system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="ServiceBehavior" name="Service">  
        <endpoint address="" binding="wsHttpBinding" contract="IService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
  </system.serviceModel>

和Final我的Jquery调用是

var ID = 10;
        var UnitNumber = '10001';
        var UnitType = 1;

        function CallService() {
           var input =
            {
                    ID: "10",
                    UnitNumber: "10001",
                    UnitType: "1"
            };

            //alert(JSON.stringify(input));
            $.ajax({
                type: "POST",
                url: "http://localhost:2878/Mtel_Profiler/Service.svc/getCommandStatus",
                data: JSON.stringify(input),
                contentType: "application/json",
                success: function (userViewModel) {
                    ServiceSucceeded(userViewModel);
                },
                error: function(msg) {
                    ServiceFailed(msg);
                }
            });
        }

        function ServiceSucceeded(msg)
        {
            alert('Sucess');
            return false;
        }

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

任何人都可以帮助解决此错误。提前谢谢。

0 个答案:

没有答案