1

我可以使用简单的 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);
            }
        });
    }
4

1 回答 1

2

这可能是因为您正在执行跨站点脚本,假设这http://localhost:8733也不是您页面的地址(端口有所不同)。

请改用 JSONP。你的回复应该是这样的:

myCallback(['your data structure here'])

myCallback()JavaScript 中的有效函数在哪里。$.ajax调用也需要这个:

dataType: 'jsonp'

另请参阅:远程 JSON

于 2012-12-03T23:40:20.933 回答