Home  >  Article  >  Web Front-end  >  How jquery calls wcf and displays data_jquery

How jquery calls wcf and displays data_jquery

WBOY
WBOYOriginal
2016-05-16 18:05:031607browse

Chuangneng wcf is the first choice, the code is very simple, as follows:

Copy the code The code is as follows:

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
{
// To use HTTP GET, add the [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)
// To create an operation that returns XML,
// add [WebGet(ResponseFormat=WebMessageFormat.Xml)],
// and include the following in the operation body Line:
// 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)
{
// here Add operation implementation
DataSet ds = WebBase.GetDS(WebBase.meinvRepository, string.Empty, " body_id desc",pz);
if (ds.Tables.Count > 0)
return ds. Tables[0];
return null;
}

// Add more operations here and mark them with [OperationContract]
}


Explain that wcf has a GetDataTable method. There is a parameter pz indicating the number of rows of returned data. The returned table is used as json data for jquery

The page calling method is also very simple:

Copy code The code is as follows:

<%@ Page Language="C#" EnableViewState="false " AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="jquerywcf_Default" %>



















Explain that the data returned by wcf is mainly obtained through the getjson method of jquery. The parameter passing method:

$.getJSON(url, { pz: "6" }, function (msg) {alert(msg.d)});Here are the 6 pieces of data returned and the attributes added to the svc of wcf:
Factory=System.ServiceModel.Activation.WebScriptServiceHostFactory and the configuration of web.config:
Copy code The code is as follows:


2
3
4
5
6
7

8
9

































































< ;/pages>














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


< ;/compiler>
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空间试试 :)
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn