简体   繁体   English

JavaScript和WCF服务库之间的通信(WCF测试客户端)

[英]Communication between JavaScript and WCF Service Library (WCF Test Client)

there is a web service (WCF Service Library) when I debug the web service project (in Visual Studio) "Test Client WCF" is launched (so I guess its hosted via the Test Client). 当我调试Web服务项目(在Visual Studio中)时,有一个Web服务(WCF服务库)已启动“ Test Client WCF”(因此我猜想它是通过Test Client托管的)。 I have a web service method called "Test" which returns string. 我有一个称为“ Test”的Web服务方法,该方法返回字符串。 When I "call" that method with the Test Client WCF - it works. 当我使用Test Client WCF“调用”该方法时,它会起作用。

When I want to use browser as a client. 当我想使用浏览器作为客户端时。 I go to http://localhost:9001/Name/WebService/WebAPI and I see the web service (xml with some info about methods). 我转到http:// localhost:9001 / Name / WebService / WebAPI,然后看到Web服务(带有有关方法的一些信息的xml)。 And now I want to use JavaScript to call that Test method. 现在,我想使用JavaScript调用该Test方法。

I created a client similar to this https://stackoverflow.com/a/11404133 and I replace the sr variable (SOAP request) with a request, which is in XML part of the Test method in the "Test Client WCF" and for url I chose http://localhost:9001/Name/WebService/WebAPI . 我创建了一个与此https://stackoverflow.com/a/11404133相似的客户端,并用请求替换了sr变量(SOAP请求),该请求位于“ Test Client WCF”中Test方法的XML部分中,并且url我选择了http:// localhost:9001 / Name / WebService / WebAPI I tried that JavaScript client, but I got some client error - 我尝试使用该JavaScript客户端,但出现了一些客户端错误-

content-type 'text-xml' is invalid, server wanted 'application/soap+xml; charset=utf-8' content-type 'text-xml'无效,服务器需要'application/soap+xml; charset=utf-8' 'application/soap+xml; charset=utf-8'

(unfortunately right now I can't get to the web service, so I don't know a number of the error and exact message, but there was no other information, beside the content-type). (不幸的是,现在我无法使用Web服务,所以我不知道很多错误和确切的消息,但是除了内容类型之外,没有其他信息)。 So I changed the request header to 'application/soap+xml; charset=utf-8' 所以我将请求标头更改为'application/soap+xml; charset=utf-8' 'application/soap+xml; charset=utf-8' , but then I got error – that tells me: 'application/soap+xml; charset=utf-8' ,但随后出现错误–告诉我:

The message cannot be processed at the receiver, due to an AddressFilter mismatch at the EndpointDispatcher. 由于EndpointDispatcher上的AddressFilter不匹配,因此无法在接收方处理该消息。 Check that the sender and receiver's EndpointAddresses agree 检查发送方和接收方的EndpointAddresses是否一致

(Or something like that - I had to translated it to english) (或者类似的东西-我不得不将其翻译成英文)

I also tried the "JavaScript client" with an existing service, that I found on the internet and with text/xml content-type. 我还尝试了在Internet上使用text/xml content-type找到的现有服务的“ JavaScript客户端”。 And it works fine. 而且效果很好。

Please do you have any advice - how to call the Test method with JavaScript? 请问您有什么建议-如何使用JavaScript调用Test方法? Thanks. 谢谢。

The service invocated in Javascript is called Restful style service. 用Javascript调用的服务称为Restful样式服务。 WCF is able to create a Restful style service too. WCF也能够创建Restful风格的服务。 But we need to set up some kinds of additional configuration. 但是我们需要设置一些其他配置。 The service is hosting in IIS express when we test the service in Visual Studio. 当我们在Visual Studio中测试服务时,该服务托管在IIS express中。 It uses the default binding configuration to host the service(BasicHttpBinding), called SOAP web service. 它使用默认的绑定配置来承载称为SOAP Web服务的服务(BasicHttpBinding)。 The universal way to invocate the service is taking advantage of using service proxy class, that is what the WCFTestClient do. 调用服务的通用方法是利用服务代理类,这就是WCFTestClient的用途。
If we want to invocate the service by using JavaScript, here is a simple demo, wish it is useful to you. 如果我们想使用JavaScript调用该服务,这是一个简单的演示,希望对您有用。 Please be aware that the project template is WCF Service Application instead of Service Library project. 请注意,项目模板是WCF服务应用程序,而不是服务库项目。
https://stackoverflow.com/questions/56873239/how-to-fix-err-aborted-400-bad-request-error-with-jquery-call-to-c-sharp-wcf/56879896#56879896 https://stackoverflow.com/questions/56873239/how-to-fix-err-aborted-400-bad-request-error-with-jquery-call-to-c-sharp-wcf/56879896#56879896
Feel free to let me know if there is something I can help with. 随时让我知道是否有什么我可以帮助的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM