1

我做了一些更改,现在得到 415 Unsupported Media Type。我正在发布更新的代码。我的 Web 服务接口。

服务/IWebService.cs

    [OperationContract]
    [WebInvoke(Method = "POST",
        BodyStyle=WebMessageBodyStyle.Wrapped,
        RequestFormat=WebMessageFormat.Json,
        ResponseFormat=WebMessageFormat.Json,
        UriTemplate = "http://localhost:50571/Service/WebService.svc/hello/say")]
    string hello(Deneme deneme );

 [DataContract]
public class Deneme
{
    [DataMember]
    public string say { get; set; }
}

我的网络服务。服务/WebService.svc

 [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
 [AspNetCompatibilityRequirements(RequirementsMode =    AspNetCompatibilityRequirementsMode.Allowed)]
public class WebService : IWebService
{
    public string hello(Deneme deneme) {
        return deneme.say;
    }

客户端代码。在登录.aspx

$.ajax({
        type: 'POST',
        url: '/Service/WebService.svc/hello',
        data: { 'say': 'sdfs' },
        contentType: 'application/json; charset=utf-8',
        dataType:'json',
        success: function (s) {
            alert(s.d);
        }
    });

网页配置

<system.serviceModel>
  <services>
      <service name="TETP.Service.WebService">
          <endpoint address=""
            behaviorConfiguration=""
            binding="basicHttpBinding"
            contract="TETP.Service.IWebService" />
      </service>
  </services>
<behaviors>
  <serviceBehaviors >
    <behavior name="">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true" />
<client>
  <endpoint address="http://localhost:50571/Service/WebService.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IWebService" contract="TETP.Service.IWebService" name="BasicHttpBinding_IWebService" />
</client>

我曾经收到 400 错误请求错误。现在我收到 415 Unsupported Media Type 错误 提前谢谢你。

4

1 回答 1

0

我认为您的数据请求可能需要与您尝试的不同的引号,但我不确定。我建议在调用之前添加数据警报以进行调试。见下文..

var mData = "{userName:'" + $('#usernametxtbx').val() + "',password:'" + $('#passwordtxtbx').val() + "'}";
alert(mData);
$.ajax({
        type: 'GET',
        url: 'Service/WebService.svc/Login',
        data:{userName:$('#usernametxtbx').val(),password:$('#passwordtxtbx').val()},
        dataType: 'application/json; content=utf-8',
        contentType: 'json',
        success: function (res) {
        if(res.d == true)
            window.location.replace(ResolveUrl('Default.aspx'));
            else
                window.location.replace(ResolveUrl('Login.aspx'));
        }
    });
于 2013-07-28T01:00:39.617 回答