简体   繁体   English

使用WCF REST模板4.0,如何通过AJAX使用JSON

[英]Using WCF REST Template 4.0, How to consume JSON via AJAX

Using the WCF REST template 4, among cutting down on the amount of configuration needed, you no longer require an SVC file to host a REST Web Service. 使用WCF REST模板4,在减少所需的配置量的同时,您不再需要SVC文件来托管REST Web服务。

Previously I could use an asp:ScriptManager to create a JavaScript proxy that i could use to call my WCF Web Services using AJAX, I only had to provide the path to my SVC file. 以前,我可以使用asp:ScriptManager创建一个JavaScript代理,我可以使用它使用AJAX调用WCF Web服务,我只需要提供指向SVC文件的路径即可。

Now that there is no SVC file and the service factory is instantiated in the global.asax file, I am not sure how to call my operation contracts via JavaScript. 现在没有SVC文件,并且在global.asax文件中实例化了服务工厂,因此我不确定如何通过JavaScript调用操作合同。

Is there a way round this, or a best practice regarding doing this with WCF 4? 是否有办法解决此问题,或者是使用WCF 4进行此操作的最佳实践? Am I better using the old 3.5 model of svc files etc? 我最好使用svc文件等的旧3.5模型吗?

EDIT : Or is my best option to use jQuery $Ajax calls? 编辑:还是使用jQuery $ Ajax调用的最佳选择?

Thank you to anyone for your time. 多谢您的宝贵时间。

You were right. 你是对的。 your best option would be to use jQuery $.ajax() calls. 您最好的选择是使用jQuery $.ajax()调用。

If you happen to integrate test your service, you can try self hosting it and creating a proxy using ChannelFactory<T>() where T is your contract type. 如果您碰巧要集成测试服务,则可以尝试对其进行自我托管,并使用ChannelFactory<T>()创建代理,其中T是您的合同类型。

Ex: 例如:
var address = new EndpointAddress(@"http://localhost:8080/YourWebService/Section/"); var address = new EndpointAddress(@“ http:// localhost:8080 / YourWebService / Section /”);
var factory = new ChannelFactory(new WebHttpBinding(), address); var factory = new ChannelFactory(new WebHttpBinding(),address); // Use your desired binding here //在此处使用所需的绑定
factory.Endpoint.Behaviors.Add(new WebHttpBehavior()); factory.Endpoint.Behaviors.Add(new WebHttpBehavior()); // Add your desired behaviors here //在此处添加所需的行为
var wcfProxy = factory.CreateChannel(); var wcfProxy = factory.CreateChannel();

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

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