$ .ajax()POST命令未连接到WCF服务

时间:2012-12-03 23:11:12

标签: c# jquery ajax wcf rest

我可以使用简单的表单POST连接到服务,但$ .ajax方法不起作用,我收到错误:“没有传输”

接口/合同:

[ServiceContract]
public interface IService1
{

    [OperationContract]
    Msg updateMessageJSON(Msg message);
}

datacontract和数据成员:

[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);
            }
        });
    }

1 个答案:

答案 0 :(得分:2)

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

改用JSONP。您的回答应该是:

myCallback(['your data structure here'])

myCallback()是JavaScript中的有效函数。 $.ajax调用也需要此功能:

dataType: 'jsonp'

另请参阅:Remote JSON