来自JQuery的Wcf

来自JQuery的Wcf,jquery,wcf,Jquery,Wcf,我试图从jQueryAjax调用wcf服务。服务正常您好: 网络服务: [ServiceContract] public interface IHelloService { [OperationContract] [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)] string GetMessag

我试图从jQueryAjax调用wcf服务。服务正常您好:

网络服务

[ServiceContract]
public interface IHelloService
{
    [OperationContract]
   [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat =      WebMessageFormat.Json)]

    string GetMessage(string nome);

}

public class HelloService : IHelloService
{
    public string GetMessage(string nome)
    {
        return "Hello " + nome + "!";
    }
}
网络配置

<system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="">
                    <serviceMetadata httpGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="false" />
                </behavior>
            </serviceBehaviors>

        </behaviors>

        <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
            multipleSiteBindingsEnabled="true" />
    </system.serviceModel>
<script>
        $('#Button1').click(function () {
            $.ajax({
                url: "http://localhost:2021/HelloService.svc/GetMessage",
                type: "POST",
                data: JSON.stringify($('#TextBox1').val()),
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    alert("ciao");
                    $('#Label1').html('Hello ' + data + '!');
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(textStatus);
                }
            });
        });
    </script>
<system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="EndpBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <client>
      <endpoint address="http://localhost:2021/HelloService.svc" binding="webHttpBinding"
          contract="HelloServiceReference.IHelloService"
          behaviorConfiguration="EndpBehavior" />
   </client>

</system.serviceModel>

消费者

<system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="">
                    <serviceMetadata httpGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="false" />
                </behavior>
            </serviceBehaviors>

        </behaviors>

        <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
            multipleSiteBindingsEnabled="true" />
    </system.serviceModel>
<script>
        $('#Button1').click(function () {
            $.ajax({
                url: "http://localhost:2021/HelloService.svc/GetMessage",
                type: "POST",
                data: JSON.stringify($('#TextBox1').val()),
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    alert("ciao");
                    $('#Label1').html('Hello ' + data + '!');
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(textStatus);
                }
            });
        });
    </script>
<system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="EndpBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <client>
      <endpoint address="http://localhost:2021/HelloService.svc" binding="webHttpBinding"
          contract="HelloServiceReference.IHelloService"
          behaviorConfiguration="EndpBehavior" />
   </client>

</system.serviceModel>

$('#按钮1')。单击(函数(){
$.ajax({
url:“http://localhost:2021/HelloService.svc/GetMessage",
类型:“POST”,
数据:JSON.stringify($('#TextBox1').val(),
数据类型:“json”,
contentType:“应用程序/json;字符集=utf-8”,
成功:功能(数据){
警报(“ciao”);
$('#Label1').html('Hello'+data+'!');
},
错误:函数(XMLHttpRequest、textStatus、errorshown){
警报(文本状态);
}
});
});
Web.config消费者

<system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="">
                    <serviceMetadata httpGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="false" />
                </behavior>
            </serviceBehaviors>

        </behaviors>

        <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
            multipleSiteBindingsEnabled="true" />
    </system.serviceModel>
<script>
        $('#Button1').click(function () {
            $.ajax({
                url: "http://localhost:2021/HelloService.svc/GetMessage",
                type: "POST",
                data: JSON.stringify($('#TextBox1').val()),
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    alert("ciao");
                    $('#Label1').html('Hello ' + data + '!');
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(textStatus);
                }
            });
        });
    </script>
<system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="EndpBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <client>
      <endpoint address="http://localhost:2021/HelloService.svc" binding="webHttpBinding"
          contract="HelloServiceReference.IHelloService"
          behaviorConfiguration="EndpBehavior" />
   </client>

</system.serviceModel>

这给了我错误。“success”中的代码不运行。 我尝试使用asp.net consumer,它就可以了。

我找到了本教程

只有当消费者在服务项目中时,它才会运行

当我尝试运行web服务时,Visual Studio web客户端是空的。 如果我从web配置中删除端点,它就会运行,但消费者不会转到外部消费者)

这是web.config

<system.serviceModel>
    <services>
      <service name="Books" behaviorConfiguration="MyServiceBehavior">

        <endpoint
            address=""
            binding="webHttpBinding"
            behaviorConfiguration="webEndPointBehavior"
            name="webEndPoint"
            contract="IBooks"/>

      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="MyServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>

      <endpointBehaviors>
        <behavior name="webEndPointBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>

    </behaviors>

    <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>

我找到了本教程

只有当消费者在服务项目中时,它才会运行

当我尝试运行web服务时,Visual Studio web客户端是空的。 如果我从web配置中删除端点,它就会运行,但消费者不会转到外部消费者)

这是web.config

<system.serviceModel>
    <services>
      <service name="Books" behaviorConfiguration="MyServiceBehavior">

        <endpoint
            address=""
            binding="webHttpBinding"
            behaviorConfiguration="webEndPointBehavior"
            name="webEndPoint"
            contract="IBooks"/>

      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="MyServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>

      <endpointBehaviors>
        <behavior name="webEndPointBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>

    </behaviors>

    <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>


您是否尝试过
JSON.stringify({nome:$('#TextBox1').val()})
它给了我错误
。。。什么错误?错误告诉您JSON.stringify({nome:$('#TextBox1').val()})的相同错误是什么。它只说“error”,你试过
JSON.stringify({nome:$('#TextBox1').val()})
它给了我错误
。。。什么错误?错误告诉您JSON.stringify({nome:$('#TextBox1').val()})的相同错误是什么。它只说“错误”