简体   繁体   English

MVC,WCF ASP.NET 4.0和JQUERY

[英]MVC, WCF ASP.NET 4.0 & JQUERY

I've spent the past few days getting frustrated with WCF, so I've decided to post for help on here because.. well.. I don't have a clue where to start!.. any help would be appreciated! 我过去几天对WCF感到沮丧,所以我决定在这里发布帮助,因为..很好..我不知道从哪里开始!..任何帮助将不胜感激!

Firstly: When creating a WCF Service in .Net 4.0, which template should I use if I want to be able to create a service that will accept data from an AJAX POST using JQuery? 首先:在.Net 4.0中创建WCF服务时,如果我希望能够创建使用JQuery从AJAX POST接受数据的服务,应该使用哪个模板? (I'd like to be able to have a Global.asax if possible). (如果可能,我希望能够拥有Global.asax)。

Secondly: My service works fine in the WCF Test Client, however when I manage to get it to accept GET requests, the Test Client stops showing the service methods. 其次:我的服务在WCF测试客户端中运行良好,但是当我设法使其接受GET请求时,测试客户端将停止显示服务方法。 POST methods just seem to refuse to work outright. POST方法似乎完全拒绝工作。

I'd like to develop a WCF service that will run on an IIS server that I can hook into from any one of my applications via a JQuery Ajax call. 我想开发一种WCF服务,该服务将在IIS服务器上运行,我可以通过JQuery Ajax调用将其与任何应用程序挂钩。

If anyone has a tutorial that point's me in the right direction, that would be greatly appreciated as I havn't been able to find anything on WCF using .Net 4, that works. 如果有人有指导我正确方向的教程,那将不胜感激,因为我无法使用.Net 4在WCF上找到任何有效的方法。

Cheers 干杯

The first thing that you should consider is the same origin policy restriction. 您应该考虑的第一件事是相同的原始策略限制。 If you are not able to comply with it and your web service is not hosted on the same domain as the consuming AJAX script you may stop reading my answer here and rethink your architecture. 如果您不能遵守它,并且您的Web服务未与使用AJAX脚本的主机托管在同一域中,则您可能会在这里停止阅读我的答案并重新考虑您的体系结构。

If you are still reading you could start by defining the service contract and implementation as usual: 如果您仍在阅读,可以像往常一样定义服务合同和实现:

[ServiceContract]
public interface IFoo
{
    [OperationContract]
    string GetData(int value);
}

public class FooService : IFoo
{
    public string GetData(int value)
    {
        return string.Format("You entered: {0}", value);
    }
}

Then you add a fooservice.svc file which will expose the service in IIS: 然后添加一个fooservice.svc文件,该文件将在IIS中公开该服务:

<%@ ServiceHost 
    Language="C#" 
    Debug="true" 
    Service="SomeNs.FooService" 
    CodeBehind="FooService.svc.cs" 
    Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory"
%>

The last line Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory" is extremely important as this is what will allow you to use JSON. 最后一行Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory"非常重要,因为这使您可以使用JSON。

The last part is web.config: 最后一部分是web.config:

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

And finally an HTML page sending AJAX request to consume the service: 最后是一个发送AJAX请求以使用该服务的HTML页面:

<!DOCTYPE html>
<html>
<head>
    <title>WCF Test</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript" src="http://www.json.org/json2.js"></script>
    <script type="text/javascript">
        $(function () {
            $.ajax({
                // Notice the URL here: Need to be hosted on the same domain
                url: '/fooservice.svc/getdata',
                type: 'post',
                contentType: 'application/json; charset=utf-8',
                data: JSON.stringify({ value: 7 }),
                success: function (result) {
                    alert(result.d);
                }
            });
        });
    </script>
</head>
<body>

</body>
</html>

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

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