jquery 调用wcf

<head runat="server">
    <script src="Scripts/jquery-1.9.1.js" type="text/javascript"></script>
 <script type="text/javascript">

     $(document).ready(function () {
         var nxtIdx = $('input:text').index(this) + 1;

         $("#sendtest").click(function () {

             $.ajax({
                 type: "post",
                 url: "http://localhost/Service1.svc/GetData",
                 data: '{"value":"' + 2 + '"}',
                 contentType: 'text/json',
                 dataType: "json",
                 success: function (data) {
                     $("#getContent").val(data.d.toString());
                 },
                 error: function (data) {
                     alert(data.statusText);
                 }
             });

         });
     });

        
   
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <input type="button" id="sendtest" value="Click" />

    <input type="text" id="getContent" />
    </div>
    </form>
</body>
</html>



调用的wcf是在另外的项目中。包含:IService1.cs 和 Service1.cs

接口:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;

namespace WcfService1
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。
    [ServiceContract]
 
    public interface IService1
    {

        [OperationContract]
        [WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        string GetData(int value);

        [OperationContract]
        [WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        string GetDataNone();

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        // TODO: 在此添加您的服务操作
    }


    // 使用下面示例中说明的数据约定将复合类型添加到服务操作。
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
}

server:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;

namespace WcfService1
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“Service1”。
     
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public string GetDataNone()
        {
            return string.Format("You entered: {0}", 1);
        }
        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
    }
}

SVC :WCF服务文件

<%@ ServiceHost Language="C#" Debug="true" Service="WcfService1.Service1" %>
<%@Assembly Name="WcfService1" %>


注意:1:wcf不要在IIS中发布,才可以调用成功,否则会报错(当wcf服务和调用的页面在不同的项目中,就会报错,如果在同一个项目,wcf不需要单独在iis上发布)。我开始就花了很长时间在这个问题上,一直以为自己写的代码有问题,其实只要iis发布一下就可以了。

2:调用wcf 的错误,基本出在要传递的参数上,要多注意下下参数的拼接。

3:wcf 后端不管传递字符串,json,以及对象给前端,都是自定义的,前端 都可以得到返回值得。同样后端传递object给后端,后端的参数类也会自动取得

3:wcf里的方法怎么调试,大家有什么方法,可以告诉一下我么




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值