创建一个WCF服务从jQuery.AJAX()接受JSON数据

时间:2022-10-08 10:05:04

I have been searching around for hours and trying different things to get this to work. I have tried so many articles on * and either I am too stupid to get things working or I have some unique and odd configuration that is preventing me from experiencing joy.

我一直在寻找几个小时并尝试不同的方法来实现这一目标。我已经尝试了很多关于*的文章,或者我太愚蠢了,无法让事情发生,或者我有一些独特而奇怪的配置让我无法体验到快乐。

I create the WCF service outlined by this tutorial:

我创建了本教程概述的WCF服务:

http://www.codeproject.com/Articles/97204/Implementing-a-Basic-Hello-World-WCF-Service

http://www.codeproject.com/Articles/97204/Implementing-a-Basic-Hello-World-WCF-Service

It is super basic and has one method and all I want it to do is allow me to consume it with jQuery.AJAX() using json.

它是超级基础的并且有一个方法,我想要它做的就是允许我使用json使用jQuery.AJAX()。

I have it hosted in IIS and it works. I can access the WSDL without issues.

我把它托管在IIS中,它的工作原理。我可以毫无问题地访问WSDL。

I try to consume it with the following code:

我尝试使用以下代码使用它:

$.ajax({
    dataType: 'json',
    type: 'POST',
    contentType: "application/json",
    url: "//localhost:546/HelloWorldService.svc/GetMessage",
    data: {
        name: "Joe"
    }
    }).done(function(msg){
        console.log(msg);
        $("#result").append(msg);
});

I always get errors. Based on what I have tried I get 500 errors, 402 errors, errors about incorrect content... All the errors.

我总是得到错误。根据我的尝试,我得到500错误,402错误,错误内容错误...所有错误。

I have tried implementing solutions from the following articles. They range from having me change the web.config endpoints (I know I HAVE to change them but nothing I have tried so far works in terms of adding a JSON endpoint) to adding things like

我尝试过以下文章中的解决方案。它们包括让我更改web.config端点(我知道我必须更改它们但我迄今为止没有尝试过添加JSON端点的工作)来添加像

[WebInvoke(Method = "POST", UriTemplate = "json/PostSalesOrderData", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]

to the interface.

到界面。

Here are some of the articles I have looked at and tried to smash into my solution to get it working without much success.

以下是我看过的一些文章,并试图粉碎我的解决方案,使其工作没有太大的成功。

Javascript JSON and WCF webservice on Phonegap Android

Phonegap Android上的Javascript JSON和WCF Web服务

HTTP/1.1 415 Cannot process the message because the content type 'application/json; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'

HTTP / 1.1 415无法处理消息,因为内容类型为'application / json; charset = utf-8'不是预期的类型'text / xml;字符集= UTF-8'

WCF Services with JSON, JSONP and SOAP End Points

具有JSON,JSONP和SOAP端点的WCF服务

Two endpoint (soap ,json) and one service method

两个端点(soap,json)和一个服务方法

WCF REST Service not accepting JSON in .Net 4

WCF REST服务不接受.Net 4中的JSON

I also went through this tutorial and tried to use what he had to say to get my solution working. Still nothing!

我也经历了这个教程并尝试使用他必须说的来让我的解决方案正常工作。依然没有!

http://www.primordialcode.com/blog/post/passing-json-serialized-objects-wcf-service-jquery

http://www.primordialcode.com/blog/post/passing-json-serialized-objects-wcf-service-jquery

This is how my interface looks

这就是我的界面的样子

[ServiceContract]
public interface IHelloWorldService
{
    [OperationContract]
    [WebInvoke(UriTemplate = "GetMessage", Method = "POST",
        BodyStyle = WebMessageBodyStyle.WrappedRequest,
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json)]
    String GetMessage(String name);
}

Can anyone help me experience joy?

任何人都可以帮我体验快乐吗?

Thanks in advance for even looking at my question. If you need more information or I have not provided enough let me know so I can help you help me!

提前感谢您查看我的问题。如果您需要更多信息或我没有提供足够的信息,请告诉我,以便我帮助您!

I must be missing something stupid... I know it is not this hard.

我一定错过了一些愚蠢的东西......我知道这不是很难。

EDIT:

编辑:

Working Web.Config

工作Web.Config

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="false" targetFramework="4.0" />
  </system.web>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="WebHTTPEndpointBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="MyServiceTypeBehaviors">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <webHttpBinding>
        <binding name="MyWebServiceBinding" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"/>
      </webHttpBinding>
    </bindings>
    <services>
      <service name="MyWCFServices.HelloWorldService"
       behaviorConfiguration="MyServiceTypeBehaviors">
        <endpoint address="" binding="webHttpBinding" bindingConfiguration="MyWebServiceBinding" behaviorConfiguration="WebHTTPEndpointBehavior"
          contract="MyWCFServices.IHelloWorldService"/>
        <endpoint contract="IMetadataExchange"
          binding="mexHttpBinding" address="mex"/>
      </service>
    </services>
  </system.serviceModel>
</configuration>

2 个解决方案

#1


4  

change this line

改变这一行

data: {
    name: "Joe"
}

to

data: JSON.stringify({name: 'Joe'});

EDIT:

编辑:

Do this to your service. Add WebHttp binding in the config.

这样做是为了您的服务。在配置中添加WebHttp绑定。

 <behaviors>
  <endpointBehaviors>
    <behavior>
      <webHttp />
    </behavior>
  </endpointBehaviors>
</behaviors>

Hope you know where to add this. If not let me know and I will try to provide some inputs.

希望你知道在哪里添加这个。如果不让我知道,我会尝试提供一些意见。

EDIT:

编辑:

Following up on my comment,

跟进我的评论,

<behavior name="myBehaviorName">
      <webHttp />
</behavior>

<service name="MyWCFServices.HelloWorldService"
         behaviorConfiguration="MyServiceTypeBehaviors">
    <endpoint address="" binding="webHttpBinding"
         contract="MyWCFServices.IHelloWorldService" behaviorConfiguration="myBehaviorName"/>
    <endpoint contract="IMetadataExchange"
       binding="mexHttpBinding" address="mex"/>
  </service>

#2


1  

I'm coming in late, but you still have some issues you'll need to resolve to get it to work. You need to change the binding for your endpoint to support the HTTP operations.

我来晚了,但是你仍然需要解决一些问题才能让它发挥作用。您需要更改端点的绑定以支持HTTP操作。

<bindings>
    <webHttpBinding>
        <binding name="MyWebServiceBinding" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"/>
    </webHttpBinding>
</bindings>
<behaviors>
    <endpointBehaviors>
        <behavior name="WebHTTPEndpointBehavior">
          <webHttp helpEnabled="true"/>
        </behavior>
    </endpointBehaviors>
</behaviors>
<services>
  <service name="MyWCFServices.HelloWorldService"
         behaviorConfiguration="MyServiceTypeBehaviors">
    <endpoint address="" binding="webHttpBinding" bindingConfiguration="MyWebServiceBinding" behaviorConfiguration="WebHTTPEndpointBehavior"
         contract="MyWCFServices.IHelloWorldService"/>
    <endpoint contract="IMetadataExchange"
       binding="mexHttpBinding" address="mex"/>
  </service>
</services>

The maxBufferSize and maxReceivedMessageSize is optional.

maxBufferSize和maxReceivedMessageSize是可选的。

EDIT: Oops, forgot to add your behaviorConfiguration in.

编辑:哎呀,忘了添加你的behaviorConfiguration。

#1


4  

change this line

改变这一行

data: {
    name: "Joe"
}

to

data: JSON.stringify({name: 'Joe'});

EDIT:

编辑:

Do this to your service. Add WebHttp binding in the config.

这样做是为了您的服务。在配置中添加WebHttp绑定。

 <behaviors>
  <endpointBehaviors>
    <behavior>
      <webHttp />
    </behavior>
  </endpointBehaviors>
</behaviors>

Hope you know where to add this. If not let me know and I will try to provide some inputs.

希望你知道在哪里添加这个。如果不让我知道,我会尝试提供一些意见。

EDIT:

编辑:

Following up on my comment,

跟进我的评论,

<behavior name="myBehaviorName">
      <webHttp />
</behavior>

<service name="MyWCFServices.HelloWorldService"
         behaviorConfiguration="MyServiceTypeBehaviors">
    <endpoint address="" binding="webHttpBinding"
         contract="MyWCFServices.IHelloWorldService" behaviorConfiguration="myBehaviorName"/>
    <endpoint contract="IMetadataExchange"
       binding="mexHttpBinding" address="mex"/>
  </service>

#2


1  

I'm coming in late, but you still have some issues you'll need to resolve to get it to work. You need to change the binding for your endpoint to support the HTTP operations.

我来晚了,但是你仍然需要解决一些问题才能让它发挥作用。您需要更改端点的绑定以支持HTTP操作。

<bindings>
    <webHttpBinding>
        <binding name="MyWebServiceBinding" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"/>
    </webHttpBinding>
</bindings>
<behaviors>
    <endpointBehaviors>
        <behavior name="WebHTTPEndpointBehavior">
          <webHttp helpEnabled="true"/>
        </behavior>
    </endpointBehaviors>
</behaviors>
<services>
  <service name="MyWCFServices.HelloWorldService"
         behaviorConfiguration="MyServiceTypeBehaviors">
    <endpoint address="" binding="webHttpBinding" bindingConfiguration="MyWebServiceBinding" behaviorConfiguration="WebHTTPEndpointBehavior"
         contract="MyWCFServices.IHelloWorldService"/>
    <endpoint contract="IMetadataExchange"
       binding="mexHttpBinding" address="mex"/>
  </service>
</services>

The maxBufferSize and maxReceivedMessageSize is optional.

maxBufferSize和maxReceivedMessageSize是可选的。

EDIT: Oops, forgot to add your behaviorConfiguration in.

编辑:哎呀,忘了添加你的behaviorConfiguration。