weixin_33713707 2010-10-07 15:38 采纳率: 0%
浏览 56

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!

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? (I'd like to be able to have a Global.asax if possible).

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. POST methods just seem to refuse to work outright.

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.

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.

Cheers

  • 写回答

1条回答 默认 最新

  • 游.程 2010-10-07 16:41
    关注

    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.

    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:

    <%@ 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.

    The last part is 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:

    <!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>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献