解決WCF跨域問題,及DataTable參數(shù)問題
上一篇章配置了如何可以使用http方式調(diào)用wcf,在C#代碼中可以使用HttpClient,HttpWebRequest等web類進(jìn)行請求
但是如果直接使用js發(fā)送ajax請求的時候卻會出現(xiàn)跨域問題
首先是web.config中的system.serviceModel節(jié)點配置
如下:
<system.serviceModel> <services> <service name="WcfService.ServiceAjax"> <endpoint address="" behaviorConfiguration="WcfService.ServiceAjaxAspNetAjaxBehavior" binding="webHttpBinding" contract="WCFlib.IClass" bindingConfiguration="webBinding"/> </service> </services> <bindings> <webHttpBinding> <binding name="webBinding" crossDomainScriptAccessEnabled="true" > </binding> </webHttpBinding> </bindings> <behaviors> <endpointBehaviors> <behavior name="WcfService.ServiceAjaxAspNetAjaxBehavior" > <webHttp automaticFormatSelectionEnabled="true" defaultBodyStyle="Wrapped" helpEnabled="true" defaultOutgoingResponseFormat="Json" faultExceptionEnabled="true"/> <!--<enableWebScript/> 這一個不能要,否則就不能使用ref或out參數(shù)了--> <dataContractSerializer ignoreExtensionDataObject="true"/> </behavior> </endpointBehaviors> <serviceBehaviors> <behavior name=""> <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="true" /> </behavior> </serviceBehaviors> </behaviors> <protocolMapping> <add binding="basicHttpsBinding" scheme="https" /> <add binding="basicHttpsBinding" scheme="http" /> <add binding="webHttpBinding" scheme="https"/> <add binding="webHttpBinding" scheme="http"/> </protocolMapping> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> </system.serviceModel>
主要的配置在service的endpoint里面的behaviorConfiguration,關(guān)于配置文件的詳解,請查看上一篇
然后在wcf項目中添加一個全局配置文件

在里面的Application_BeginRequest方法中寫入
protected void Application_BeginRequest(object sender, EventArgs e) { HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*"); if (HttpContext.Current.Request.HttpMethod != "OPTIONS") return; HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST"); HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept"); HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000"); HttpContext.Current.Response.End(); }
之后就可以使用了,當(dāng)然,契約接口和契約實現(xiàn)類,也請參照上一篇章
讓W(xué)CF支持Http調(diào)用
下面是調(diào)用的html文件
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8" /> <title></title> <script crossorigin="anonymous" integrity="sha512-WNLxfP/8cVYL9sj8Jnp6et0BkubLP31jhTG9vhL/F5uEZmg5wEzKoXp1kJslzPQWwPT1eyMiSxlKCgzHLOTOTQ==" src="https://lib.baomitu.com/jquery/3.5.1/jquery.js"></script> <script> $(function () { $('#btn').click(function () { $.ajax({ url: 'http://localhost:15500/ServiceAjax.svc/TestModel', type:'POST', data: '{\"name\":\"aaa\"}', async: false, dataType: "json", Accept:'*/*', contentType: 'application/json', success: function (data) { console.log(data) }, error: function (data) { console.log(data) } } ) }) }) </script> </head> <body> <button id="btn">Get</button> </body> </html>
下面是成功的截圖

==========================
關(guān)于DataTable的參數(shù)問題
如果有是out類的則無須管
但如果是入?yún)?例如以下方法
public string ParamterTable(DataTable table) { return "傳遞成功"; }
傳遞table的時候,應(yīng)該如下傳遞 首先封裝成一個json,然后DataTable的數(shù)據(jù),需要是一個xml文檔
該文檔如下所示
<!--命名空間為固定的,必須如此,表示此處是一個DataTable類型--> <DataTable xmlns=\"http://schemas.datacontract.org/2004/07/System.Data\"> <!--schema定義DataTable的架構(gòu)--> <xs:schema id=\"NewDataSet\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"\" xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\"> <xs:element name=\"NewDataSet\" msdata:IsDataSet=\"true\" msdata:MainDataTable=\"table\" msdata:UseCurrentLocale=\"true\"> <xs:complexType> <xs:choice minOccurs=\"0\" maxOccurs=\"unbounded\"> <xs:element name=\"table\"> <xs:complexType> <xs:sequence> <xs:element name=\"ID\" type=\"xs:string\" minOccurs=\"0\"/> <xs:element name=\"Name\" type=\"xs:string\" minOccurs=\"0\"/> </xs:sequence> </xs:complexType> </xs:element> </xs:choice> </xs:complexType> </xs:element> </xs:schema> <!--定義DataTable的內(nèi)容--> <diffgr:diffgram xmlns:diffgr=\"urn:schemas-microsoft-com:xml-diffgram-v1\" xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\"> <DocumentElement xmlns=\"\"> <!--table為此DataTable的名稱,下面就是每一行的數(shù)據(jù)--> <!--注意diffgr:hasChanges特性,此處可取值 modified 表示該行已修改 inserted 表示該行為新添加--> <table diffgr:id=\"table1\" msdata:rowOrder=\"0\" diffgr:hasChanges=\"modified\"> <ID>0</ID> <Name>33</Name> </table> <table diffgr:id=\"table3\" msdata:rowOrder=\"2\"> <ID>2</ID> <Name>3</Name> </table> <table diffgr:id=\"table4\" msdata:rowOrder=\"3\"> <ID>3</ID> <Name>4</Name> </table> <table diffgr:id=\"table5\" msdata:rowOrder=\"4\"> <ID>4</ID> <Name>5</Name> </table> <table diffgr:id=\"table6\" msdata:rowOrder=\"5\" diffgr:hasChanges=\"inserted\"> <ID>1</ID> <Name>2</Name> </table> </DocumentElement> <!--此處為已經(jīng)被刪除的行標(biāo)識 before表示該行之前的狀態(tài)--> <diffgr:before> <!--表示將原有下標(biāo)為1的行刪除了,注意看上面沒胡rowOrder=1的數(shù)據(jù)--> <table diffgr:id=\"table1\" msdata:rowOrder=\"0\" xmlns=\"\"> <ID>0</ID> <Name>1</Name> </table> <!--然后將之前第第二行下標(biāo)為2的數(shù)據(jù)放到下標(biāo)1上,而上面其實還將下標(biāo)進(jìn)行了升級--> <table diffgr:id=\"table2\" msdata:rowOrder=\"1\" xmlns=\"\"> <ID>1</ID> <Name>2</Name> </table> </diffgr:before> </diffgr:diffgram> </DataTable>
這是方法調(diào)用截圖示例

總的來說,DataTable的參數(shù)傳遞和解析都是很麻煩的,如果是C#代碼還好,解析和封裝都不算困難,但如果是JS解析的話,那就很麻煩了

浙公網(wǎng)安備 33010602011771號