ajax呼叫WCF服務

2021-06-04 22:54:12 字數 1839 閱讀 6752

關於ajax呼叫wcf服務分為跨域和不跨域兩種方式,今天咱們先介紹下不跨域下的呼叫方法。demo是在vs2008寫的.

經過測試與研究,發現ajax呼叫wcf服務必須滿足以下條件

1.wcf的通訊方式必須使用webhttpbinding

2.必須設定節點的值

3.服務的實現必須新增

[aspnetcompatibilityrequirements(requirementsmode = aspnetcompatibilityrequirementsmode.allowed)] 標記

4.方法前面必須新增如下標記

[webinvoke(method = "post", bodystyle = webmessagebodystyle.bare, responseformat = webmessageformat.json)]

5.ajax方法中傳遞的引數名稱必須和wcf服務中提供的引數方法名稱一致

以下是本人寫的**,標記顏色的是需要注意的地方

伺服器端配置檔案**

伺服器端**

[servicecontract]

public inte***ce iservice1

// 使用下面示例中說明的資料約定將復合型別新增到服務操作。

[datacontract]

public class city

set}

[datamember]

public string cityname

set

}[datamember]

public int seq

set}

}

實現**

[aspnetcompatibilityrequirements(requirementsmode = aspnetcompatibilityrequirementsmode.allowed)]

public class service1 : iservice1

", value);

}#region iservice1 成員

[webinvoke(method = "post", bodystyle = webmessagebodystyle.bare, responseformat = webmessageformat.json)]

public city getdatausingdatacontract(city composite)

[webinvoke(method = "post", bodystyle = webmessagebodystyle.bare, responseformat = webmessageformat.json)]

public listgetlist()

[webinvoke(method = "post", bodystyle = webmessagebodystyle.bare, responseformat = webmessageformat.json)]

public listgetlistdata(listlist)

#endregion

}

客戶端呼叫**

<%@ page language="c#" autoeventwireup="true" codebehind="webform1.aspx.cs" inherits="wcfservicedemoone.webform1" %>

動態呼叫WCF服務

動態呼叫wcf服務,只需要提供 svc位址,1 命名空間 using system.servicemodel.channels using system.servicemodel 2 建立訪問類invokecontext public class invokecontext public stati...

動態呼叫WCF服務

前言 由於wcf發布後,伺服器位址隨時變化,引用服務的程式就需要改變引用url才可訪問,此文記載動態呼叫wcf服務 1 發布wcf參考前文 2 新建一winform程式,新增引用system.servicemodel 3 使用工具svcutil.exe工具,工具位置c program files x...

動態呼叫WCF服務

本文 客戶端呼叫wcf 有時需要動態的呼叫服務端的wcf中的方法,本方法,反射wcf 的介面,動態呼叫介面中的方法。主要為,動態繫結,反射動態呼叫。publicstaticobjectexecutemethod stringpurl,stringpmethodname,paramsobject pp...