【问题标题】:Asp.Net Web Service 415 Unsupported Media Type errorAsp.Net Web 服务 415 不支持的媒体类型错误
【发布时间】:2013-07-27 21:09:15
【问题描述】:

我做了一些更改,现在得到 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;
    }

客户端代码。 在 Login.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 错误 提前谢谢你。

【问题讨论】:

    标签: asp.net wcf web-services


    【解决方案1】:

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

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

    【讨论】:

    • 这不是它。我发送 json 我也可以只做 var data = {userName:$('#usernametxtbx').val()} 并发送数据
    猜你喜欢
    • 2014-09-15
    • 2018-10-13
    • 2018-07-25
    • 2016-09-09
    • 2017-08-28
    • 1970-01-01
    • 2021-03-31
    • 2018-08-20
    • 2021-10-12
    相关资源
    最近更新 更多