MVC,WCF ASP.NET 4.0和放大器; JQUERY [英] MVC, WCF ASP.NET 4.0 & JQUERY

查看:163
本文介绍了MVC,WCF ASP.NET 4.0和放大器; JQUERY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了几天感到沮丧与WCF,所以我决定发帖求助就在这里,因为..好..我没有线索从哪里开始!..任何帮助将是AP preciated!

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!

首先:在.NET 4.0中,哪个模板我应该使用如果我希望能够创建一个使用JQuery接受来自一个AJAX POST数据服务创建一个WCF服务? (我希望能有一个Global.asax中如果可能的话)。

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

其次:我的业务工作正常WCF测试客户端,但是当我设法得到它接受GET请求,测试客户端将停止显示该服务的方法。 POST方法似乎只是拒绝工作顾左右而言他。

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.

我想开发一个WCF服务将运行,我可以从通过JQuery的Ajax调用我的应用程序的任何一个挂接到IIS服务器上。

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.

如果任何人有一个教程这一点的我朝着正确的方向发展,这将是极大的AP preciated因为我没有带能够找到WCF使用.NET 4中,该工作什么。

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.

干杯

推荐答案

这是你应该考虑的第一件事是同源策略限制。如果你不能遵守它,您的Web服务是不是托管在同一个域中的消费AJAX脚本,你可以停在这里看我的答案,重新审视自己的架构。

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);
    }
}

然后添加一个 fooservice.svc 文件,该文件将暴露在IIS服务:

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

最后一行厂=System.ServiceModel.Activati​​on.WebScriptServiceHostFactory是非常重要的,因为这是可以让你使用JSON。

The last line Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory" is extremely important as this is what will allow you to use JSON.

最后一部分是web.config中:

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>

最后一个HTML页面发送Ajax请求使用该服务:

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>

这篇关于MVC,WCF ASP.NET 4.0和放大器; JQUERY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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