php 调用wcf 服务,jquery调用wcf并展示出数据的方法

首选创能wcf,代码很简单,如下:

using System;

using System.Data;

using System.ServiceModel;

using System.ServiceModel.Activation;

using System.ServiceModel.Web;

using System.Web.Script.Services;

[ServiceContract(Namespace = "")]

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

[ServiceBehavior(IncludeExceptionDetailInFaults = true)]

public class imgService

{

// 要使用 HTTP GET,请添加 [WebGet] 特性。(默认 ResponseFormat 为 WebMessageFormat.Json)

// 要创建返回 XML 的操作,

// 请添加 [WebGet(ResponseFormat=WebMessageFormat.Xml)],

// 并在操作正文中包括以下行:

// WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";

[OperationContract]

[WebGet(RequestFormat=WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json)]

//[WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,BodyStyle = WebMessageBodyStyle.WrappedRequest)]

public DataTable GetDataTable(string pz)

{

// 在此处添加操作实现

DataSet ds = WebBase.GetDS(WebBase.meinvRepository, string.Empty, " body_id desc",pz);

if (ds.Tables.Count > 0)

return ds.Tables[0];

return null;

}

// 在此处添加更多操作并使用 [OperationContract] 标记它们

}

说明一下,wcf就一个GetDataTable方法,有一个参数pz表示返回数据的行数,返回的table以json数据给jquery使用

页面调用方法也很简单:

Inherits="jquerywcf_Default" %>

function sendAJAX(data) {

var url = "imgService.svc/GetDataTable";

$.getJSON(url, { pz: "6" }, function (msg) {

$(msg.d).find("Table").each(function (i) {

var rq = $(this).children("Body_AddTime").text().split('T')[0].replace("-", "").replace("-", "");

rq = rq.substring(0, rq.length - 2);

var html = '

'%20+%20%24(this).children(' + $(this).children("Body_Name").text() + '';

$(".main ul").append(html);

});

});

}

$(function () {

sendAJAX();

});

说明一下,主要是通过jquery的getjson方法去取得wcf返回的数据,传参方法:

$.getJSON(url, { pz: "6" }, function (msg) {alert(msg.d)});这里是返回6条数据还有wcf的svc上加上属性:

Factory=System.ServiceModel.Activation.WebScriptServiceHostFactory还有web.config的配置:

2

3

4

5

6

7

8

9

type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">

type="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">

type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

binding="webHttpBinding" contract="imgService" />

好的,成功!上传到gd空间试试 :)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值