我做了一些更改,现在获得415不支持的媒体类型。 我发布了更新的代码。 我的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);
}
});
Web Config
<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不支持的媒体类型错误提前谢谢。
答案 0 :(得分: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'));
}
});