我可以使用简单的 POST 表单连接到服务,但 $.ajax 方法不起作用,并且出现错误:“无传输”
接口/合约:
[ServiceContract]
public interface IService1
{
[OperationContract]
Msg updateMessageJSON(Msg message);
}
数据合同和数据成员:
[DataContract]
public class Msg{
[DataMember]
public string GUID;
[DataMember]
public string message;
}
服务等级:
public class Service1 : IService1
[WebInvoke(
Method = "POST",
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "updateMessageJSON"
)]
public Msg updateMessageJSON(Msg message)
{
using (masterEntities1 entx = new masterEntities1())
{
entx.AddToErrorMessageDatas(
new ErrorMessageData() { messageData = message.message }
);
entx.SaveChanges();
}
message.GUID = Guid.NewGuid().ToString();
return message;
}
配置文件:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" />
</system.web>
<system.serviceModel>
<services>
<service name="JSONServiceTest.Service1">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8733/Design_Time_Addresses/JSONServiceTest/Service1/" />
</baseAddresses>
</host>
<endpoint address="" binding="basicHttpBinding" contract="JSONServiceTest.IService1">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="json" binding="webHttpBinding" contract="JSONServiceTest.IService1" behaviorConfiguration="jsonBehavior"></endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="True" />
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="jsonBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
jQuery:
function exec() {
$.ajax({
url: "http://localhost:8733/Design_Time_Addresses/JSONServiceTest/Service1/json/updateMessageJSON",
type: 'POST',
data: "{ 'message':'get the new guid','GUID':''}",
contentType: "application/json; charset=utf-8",
success: function (msg) {
alert(msg);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(thrownError.responseText);
}
});
}