做一个WCF服务接受来自jQuery.AJAX JSON数据() [英] Make a WCF Service Accept JSON Data from jQuery.AJAX()

查看:225
本文介绍了做一个WCF服务接受来自jQuery.AJAX JSON数据()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找兜了几个小时尝试不同的东西,得到这个工作。我曾尝试在计算器这么多的文章,或者我太傻,把事情的工作或我有一个阻止我感觉愉悦一些独特而奇怪的配置。



我创建本教程介绍的WCF服务:



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



这是超级基本有一个方法,所有我希望它做的是让我与jQuery.AJAX()使用JSON消耗它。



我有它在IIS托管和它的作品。我可以访问WSDL没有问题



我试着用下面的代码来使用它:

  $阿贾克斯({
数据类型:'JSON',
型:POST,
的contentType:应用/ JSON,
网址: //localhost:546/HelloWorldService.svc/GetMessage,
数据:{
的名字:乔
}
})来完成(功能(MSG){
的console.log(味精);
$(#结果)追加(MSG);
});



我总是错误。根据我都试过,我得到500错误,402错误,错误有关不正确的内容......所有的错误。



我曾尝试实施从下面的文章的解决方案。它们的范围从有我改变的web.config端点(我知道我必须要改变他们,但没有到目前为止,我曾试图加入一个JSON终端方面的工作),以补充之类的东西。



  [WebInvoke(方法=POST,UriTemplate =JSON / PostSalesOrderData,RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json,BodyStyle = WebMessageBodyStyle.Bare)

在接口上。



下面是一些文章中我已经看过,并试图粉碎成我的解决办法得到它的工作没有取得多大成功。



的Javascript JSON和Android的phongap



<一个WCF web服务HREF =htt​​p://stackoverflow.com/questions/13617561/http-1-1-415-cannot-process-the-message-because-the-content-type-application-js> HTTP / 1.1 415无法处理邮件,因为内容类型应用/ JSON的;字符集= UTF-8'是不是预期的类型文本/ XML;字符集= UTF-8



WCF服务使用JSON,JSONP和SOAP终点



两个端点(SOAP,JSON)和一个服务方法



WCF REST服务不.NET 4中接受JSON



我也通过这个教程中去,并试图用他说的话让我的解决方案的工作。还是什么都没有!



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



这是怎么我的界面看上去

  [的ServiceContract] 
公共接口IHelloWorldService
{
[OperationContract的]
[WebInvoke(UriTemplate =GetMessage函数,方法=POST,
BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
字符串的GetMessage(字符串名称);
}



谁能帮助我体会快乐?



在此先感谢连看我的问题。如果您需要了解更多信息或我没有足够的提供让我知道,我可以帮你帮帮我!



我必须失去了一些愚蠢的事......我知道这是。不能这块硬



编辑:



工作的Web.Config

 <?XML版本=1.0>?; 
<结构>
<&的System.Web GT;
<编译调试=假targetFramework =4.0/>
< /system.web>
< system.webServer>
<模块runAllManagedModulesForAllRequests =真/>
< /system.webServer>
< system.serviceModel>
<&行为GT;
< endpointBehaviors>
<行为NAME =WebHTTPEndpointBehavior>
< webHttp />
< /行为>
< / endpointBehaviors>
< serviceBehaviors>
<行为NAME =MyServiceTypeBehaviors>
< serviceMetadata httpGetEnabled =真/>
< serviceDebug includeExceptionDetailInFaults =FALSE/>
< /行为>
< / serviceBehaviors>
< /行为>
<&绑定GT;
<&的WebHttpBinding GT;
<绑定名称=MyWebServiceBindingMAXBUFFERSIZE =2147483647maxReceivedMessageSize =2147483647/>
< /&的WebHttpBinding GT;
< /绑定>
<服务和GT;
<服务名称=MyWCFServices.HelloWorldService
behaviorConfiguration =MyServiceTypeBehaviors>
<端点地址=绑定=的WebHttpBindingbindingConfiguration =MyWebServiceBindingbehaviorConfiguration =WebHTTPEndpointBehavior
合同=MyWCFServices.IHelloWorldService/>
<端点合同=IMetadataExchange接口
结合=mexHttpBinding地址=MEX/>
< /服务>
< /服务>
< /system.serviceModel>
< /结构>


解决方案

改变这一行


{
的名字:乔
}

 数据> 

 数据:JSON.stringify({名称:'乔'} ); 



编辑:



这样做是为了你服务。添加WebHttp在config结合



 <&行为GT; 
< endpointBehaviors>
<&行为GT;
< webHttp />
< /行为>
< / endpointBehaviors>
< /行为>



希望你知道在哪里可以添加此。如果不是让我知道,我会尽量提供一些投入



编辑:



行为NAME =myBehaviorName>

在我的意见,

 <采取后续行动; 
< webHttp />
< /行为>

<服务名称=MyWCFServices.HelloWorldService
behaviorConfiguration =MyServiceTypeBehaviors>
<端点地址=绑定=的WebHttpBinding
合同=MyWCFServices.IHelloWorldServicebehaviorConfiguration =myBehaviorName/>
<端点合同=IMetadataExchange接口
结合=mexHttpBinding地址=MEX/>
< /服务>


I have been searching around for hours and trying different things to get this to work. I have tried so many articles on stackoverflow 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:

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.

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

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.

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

[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 Phongap Android

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'

WCF Services with JSON, JSONP and SOAP End Points

Two endpoint (soap ,json) and one service method

WCF REST Service not accepting JSON in .Net 4

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

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

<?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>

解决方案

change this line

data: {
    name: "Joe"
}

to

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

EDIT:

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

 <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>

这篇关于做一个WCF服务接受来自jQuery.AJAX JSON数据()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆