単純なフォーム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);
}
});
}