2
我從jquery調用WCF服務時遇到問題。我得到的錯誤是如何使用jQuery POST到WCF
415無法處理消息,因爲內容類型 'application/json; charset = utf-8'不是預期的類型'text/xml; charset = utf-8'。
Service1.cs
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public string Authenticate(string data)
{
return "1";
}
}
IService1
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Xml,
RequestFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "auth")]
string Authenticate(string data);
}
的app.config
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="TestServiceLibrary.Service1" behaviorConfiguration="ServiceBehaviour">
<!-- Service Endpoints -->
<!-- Unless fully qualified, address is relative to base address supplied above -->
<endpoint address ="" binding="webHttpBinding" contract="TestServiceLibrary.IService1" behaviorConfiguration="web">
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above 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="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
的test.html
<html>
<head>
<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
$(document).ready(function() {
var tests = "test";
$.ajax({
cache: false,
type: "POST",
async: false,
url: "http://localhost/TestService/Service1.svc/auth",
data: '{"data": "' + tests + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
processData: true,
success: function (msg) {
alert('success');
}
});
});
</script>
</head>
<body>
</body>
</html>
我想也設置以下參數對於這種方法,沒有運氣
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
你有沒有看到這個 - http://stackoverflow.com/a/17534969/903324? –
加入以下沒有影響 <添加工廠= 「System.ServiceModel.Activation.WebServiceHostFactory」 relativeAddress = 「./ Service1.svc」 服務= 「service.service1」/> serviceHostingEnvironment> –
[WebInvoke(Method =「POST」,BodyStyle = WebMessageBodyStyle.Bare, UriTemplate =「auth」)] –