Jquery calls wcf and shows the data method

Source: Internet
Author: User

The Code is as follows: Copy codeThe 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] feature. (The default ResponseFormat is WebMessageFormat. Json)
// Create an operation to return XML,
// Add [WebGet (ResponseFormat = WebMessageFormat. Xml)],
// The operation body contains the following lines:
// 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)
{
// Add operation implementation here
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]
}

For details, the getable able method of wcf has a parameter pz indicating the number of rows of returned data. The returned table is given to jquery as json data.

The page call method is also simple:

Copy codeThe Code is as follows: <% @ Page Language = "C #" EnableViewState = "false" AutoEventWireup = "true" CodeFile = "Default. aspx. cs"
Inherits = "jquerywcf_Default" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "Head1" runat = "server">
<Title> </title>
<Script src = "../Scripts/jquery-1.4.1-vsdoc.js" type = "text/javascript"> </script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div class = "main">
<Ul> <li> <a> </li> </ul>
</Div>
<Div>
<Script type = "text/javascript">
Function sendAJAX (data ){
Var url = "<% = _ 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 = '<li> <a href = "http://meinv.vs2010.com/topic/' + $ (this ). children ("Body_MuLuID "). text () + '/' + rq + '/show-id' + $ (this ). children ("Body_ID "). text () + '.html "target =" _ blank "> ' + $ (this ). children ("Body_Name "). text () + '</a> </li> ';
$ (". Main ul"). append (html );
});
});

}

$ (Function (){
SendAJAX ();
});
</Script>
<Div id = "msg">
</Div>
</Div>
</Form>
</Body>
</Html>

Description: The getjson method of jquery is used to retrieve the data returned by wcf. The parameter passing method is as follows:

$. GetJSON (url, {pz: "6"}, function (msg) {alert (msg. d)}). Here, 6 data records are returned, and the following attributes are added to the svc of wcf:
Factory = System. ServiceModel. Activation. WebScriptServiceHostFactory and web. config Configuration:Copy codeThe Code is as follows: <? Xml version = "1.0"?>
2 <configuration>
3
4 <configSections>
5 <! -- Ajax -->
6 <sectionGroup name = "ajaxNet">
7 <section name = "ajaxSettings" type = "AjaxPro. AjaxSettingsSectionHandler, AjaxPro.2" requirePermission = "false" restartOnExternalChanges = "true"/>
8 </sectionGroup>
9 <! -- Ajax -->
<SectionGroup name = "system. web. extensions "type =" System. web. configuration. systemWebExtensionsSectionGroup, System. web. extensions, Version = 3.5.0.0, Culture = neutral, PublicKeyToken = 31BF3856AD364E35 ">
<SectionGroup name = "scripting" type = "System. Web. Configuration. ScriptingSectionGroup, System. Web. Extensions, Version = 3.5.0.0, Culture = neutral, PublicKeyToken = 31BF3856AD364E35">
<Section name = "scriptResourceHandler" type = "System. web. configuration. scriptingScriptResourceHandlerSection, System. web. extensions, Version = 3.5.0.0, Culture = neutral, PublicKeyToken = 31BF3856AD364E35 "requirePermission =" false "allowDefinition =" MachineToApplication "/>
<SectionGroup name = "webServices" type = "System. Web. Configuration. ScriptingWebServicesSectionGroup, System. Web. Extensions, Version = 3.5.0.0, Culture = neutral, PublicKeyToken = 31BF3856AD364E35">
<Section name = "jsonSerialization" type = "System. web. configuration. scriptingJsonSerializationSection, System. web. extensions, Version = 3.5.0.0, Culture = neutral, PublicKeyToken = 31BF3856AD364E35 "requirePermission =" false "allowDefinition =" Everywhere "/>
<Section name = "profileService" type = "System. web. configuration. scriptingProfileServiceSection, System. web. extensions, Version = 3.5.0.0, Culture = neutral, PublicKeyToken = 31BF3856AD364E35 "requirePermission =" false "allowDefinition =" MachineToApplication "/>
<Section name = "authenticationService" type = "System. web. configuration. scriptingAuthenticationServiceSection, System. web. extensions, Version = 3.5.0.0, Culture = neutral, PublicKeyToken = 31BF3856AD364E35 "requirePermission =" false "allowDefinition =" MachineToApplication "/>
<Section name = "roleService" type = "System. web. configuration. scriptingRoleServiceSection, System. web. extensions, Version = 3.5.0.0, Culture = neutral, PublicKeyToken = 31BF3856AD364E35 "requirePermission =" false "allowDefinition =" MachineToApplication "/>
</SectionGroup>
</SectionGroup>
</SectionGroup>
</ConfigSections>
<AppSettings/>
<ConnectionStrings>
<Add name = "meinvvs2010ConnectionString" connectionString = "server = 127.0.0.1; user id = test; password = test; database = test; pooling = false" providerName = "MySql. data. mySqlClient "/>
</ConnectionStrings>
<! -- Ajax -->
<AjaxNet>
<AjaxSettings>
<UrlNamespaceMappings useAssemblyQualifiedName = "false" allowListOnly = "false"/>
<JsonConverters includeTypeProperty = "true"/>
<Debug enabled = "false"/>
<Token enabled = "false" sitePassword = "password"/>
</AjaxSettings>
</AjaxNet>
<Location path = "ajaxpro" allowOverride = "true" inheritInChildApplications = "false">
<System. web>
<Globalization requestEncoding = "UTF-8" responseEncoding = "UTF-8" fileEncoding = "UTF-8"/>
<! -- For iis7
<System. webServer>
<Handlers>
<Add verb = "*" path = "*. ashx" name = "AjaxPro" type = "AjaxPro. AjaxHandlerFactory, AjaxPro.2"/>
</Handlers>
</System. webServer> -->
<! -- For iis6 -->
<HttpHandlers>
<Add verb = "*" path = "*. ashx" type = "AjaxPro. AjaxHandlerFactory, AjaxPro.2"/>
</HttpHandlers>
</System. web>
</Location>
<! -- Ajax -->
<System. web>
<CustomErrors mode = "Off"/>
<Compilation debug = "true">
<Assemblies>
<Add assembly = "System. Core, Version = 3.5.0.0, Culture = neutral, PublicKeyToken = B77A5C561934E089"/>
<Add assembly = "System. Web. Extensions, Version = 3.5.0.0, Culture = neutral, PublicKeyToken = 31BF3856AD364E35"/>
<Add assembly = "System. Data. DataSetExtensions, Version = 3.5.0.0, Culture = neutral, PublicKeyToken = B77A5C561934E089"/>
<Add assembly = "System. Xml. Linq, Version = 3.5.0.0, Culture = neutral, PublicKeyToken = B77A5C561934E089"/>

<Add assembly = "System. ServiceModel, Version = 3.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089"/>
<Add assembly = "System. ServiceModel. Web, Version = 3.5.0.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35"/>
</Assemblies>

<BuildProviders>
<Remove extension = ". svc"/>
<Add extension = ". svc" type = "System. ServiceModel. Activation. ServiceBuildProvider, System. ServiceModel, Version = 3.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089"/>
</BuildProviders>
</Compilation>

<! --
You can use the <authentication> section to configure
Security Authentication mode, ASP. NET
This mode is used to identify the visitor.
-->
<Authentication mode = "Windows"/>
<! --
If an unprocessed error occurs during request execution,
Use the <mermerrors> section
You can configure the corresponding processing steps. Specifically,
In this section, developers can configure the html error page to be displayed,
To replace the error stack trace.

<CustomErrors mode = "RemoteOnly" defaultRedirect = "GenericErrorPage.htm">
<Error statusCode = "403" redirect = "NoAccess.htm"/>
<Error statusCode = "404" redirect = "FileNotFound.htm"/>
</CustomErrors>
-->
<Pages>
<Controls>
<Add tagPrefix = "asp" namespace = "System. Web. UI" assembly = "System. Web. Extensions, Version = 3.5.0.0, Culture = neutral, PublicKeyToken = 31BF3856AD364E35"/>
<Add tagPrefix = "asp" namespace = "System. web. UI. webControls "assembly =" System. web. extensions, Version = 3.5.0.0, Culture = neutral, PublicKeyToken = 31BF3856AD364E35 "/>
</Controls>
</Pages>

<HttpHandlers>
<Remove verb = "*" path = "*. asmx"/>
<Add verb = "*" path = "*. asmx "validate =" false "type =" System. web. script. services. scriptHandlerFactory, System. web. extensions, Version = 3.5.0.0, Culture = neutral, PublicKeyToken = 31BF3856AD364E35 "/>
<Add verb = "*" path = "* _ AppService. axd "validate =" false "type =" System. web. script. services. scriptHandlerFactory, System. web. extensions, Version = 3.5.0.0, Culture = neutral, PublicKeyToken = 31BF3856AD364E35 "/>
<Add verb = "GET, HEAD" path = "ScriptResource. axd "type =" System. web. handlers. scriptResourceHandler, System. web. extensions, Version = 3.5.0.0, Culture = neutral, PublicKeyToken = 31BF3856AD364E35 "validate =" false "/>
</HttpHandlers>
<HttpModules>
<Add name = "ScriptModule" type = "System. Web. Handlers. ScriptModule, System. Web. Extensions, Version = 3.5.0.0, Culture = neutral, PublicKeyToken = 31BF3856AD364E35"/>
<Add name = "ServiceModel" type = "System. ServiceModel. Activation. HttpModule, System. ServiceModel, Version = 3.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089"/>
</HttpModules>
</System. web>
<System. codedom>
<Compilers>
<Compiler language = "c #; cs; csharp" extension = ". cs" warningLevel = "4"
Type = "Microsoft. CSharp. CSharpCodeProvider, System, Version = 2.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089">
<ProviderOption name = "CompilerVersion" value = "v3.5"/>
<ProviderOption name = "WarnAsError" value = "false"/>
</Compiler>
<Compiler language = "vb; vbs; visualbasic; vbscript" extension = ". vb" warningLevel = "4"
Type = "Microsoft. VisualBasic. VBCodeProvider, System, Version = 2.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089">
<ProviderOption name = "CompilerVersion" value = "v3.5"/>
<ProviderOption name = "OptionInfer" value = "true"/>
<ProviderOption name = "WarnAsError" value = "false"/>
</Compiler>
</Compilers>
</System. codedom>
<! --
The system. webServer section runs ASP. net ajax under Internet Information Services 7.0.
Required. This section is not required for earlier versions of IIS.
-->
<System. webServer>
<Validation validateIntegratedModeConfiguration = "false"/>
<UrlCompression doStaticCompression = "true" doDynamicCompression = "true"/>

<HttpErrors errorMode = "Detailed"/>
<Asp scriptErrorSentToBrowser = "true"/>

<Modules>
<Remove name = "ScriptModule"/>
<Add name = "ScriptModule" preCondition = "managedHandler" type = "System. web. handlers. scriptModule, System. web. extensions, Version = 3.5.0.0, Culture = neutral, PublicKeyToken = 31BF3856AD364E35 "/>
</Modules>
<Handlers>
<Remove name = "WebServiceHandlerFactory-Integrated"/>
<Remove name = "ScriptHandlerFactory"/>
<Remove name = "ScriptHandlerFactoryAppServices"/>
<Remove name = "ScriptResource"/>
<Remove name = "svc"/>
<Add name = "svc" path = "*. svc "verb =" * "type =" System. serviceModel. activation. httpHandler, System. serviceModel, Version = 3.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089 "preCondition =" integratedMode "/>

<Add verb = "*" path = "*. ashx" name = "AjaxPro" type = "AjaxPro. AjaxHandlerFactory, AjaxPro.2"/>
<Add name = "ScriptHandlerFactory" verb = "*" path = "*. asmx" preCondition = "integratedMode"
Type = "System. Web. Script. Services. ScriptHandlerFactory, System. Web. Extensions, Version = 3.5.0.0, Culture = neutral, PublicKeyToken = 31BF3856AD364E35"/>
<Add name = "ScriptHandlerFactoryAppServices" verb = "*" path = "* _ AppService. axd" preCondition = "integratedMode"
Type = "System. Web. Script. Services. ScriptHandlerFactory, System. Web. Extensions, Version = 3.5.0.0, Culture = neutral, PublicKeyToken = 31BF3856AD364E35"/>
<Add name = "ScriptResource" preCondition = "integratedMode" verb = "GET, HEAD" path = "ScriptResource. axd "type =" System. web. handlers. scriptResourceHandler, System. web. extensions, Version = 3.5.0.0, Culture = neutral, PublicKeyToken = 31BF3856AD364E35 "/>
</Handlers>
</System. webServer>
<Runtime>
<AssemblyBinding appliesTo = "v2.0.50727" xmlns = "urn: schemas-microsoft-com: asm. v1">
<DependentAssembly>
<AssemblyIdentity name = "MySql. Data" publicKeyToken = "C5687FC88969C44D"/>
<! -- <BindingRedirect oldVersion = "5.0.7.0" newVersion = "6.3.2.0"/> -->
<BindingRedirect oldVersion = "5.0.7.0" newVersion = "6.2.3.0"/>
</DependentAssembly>
<DependentAssembly>
<AssemblyIdentity name = "System. Web. Extensions" publicKeyToken = "31bf3856ad364e35"/>
<BindingRedirect oldVersion = "1.0.0.0-1.1.0.0" newVersion = "3.5.0.0"/>
</DependentAssembly>
<DependentAssembly>
<AssemblyIdentity name = "System. Web. Extensions. Design" publicKeyToken = "31bf3856ad364e35"/>
<BindingRedirect oldVersion = "1.0.0.0-1.1.0.0" newVersion = "3.5.0.0"/>
</DependentAssembly>
</AssemblyBinding>
</Runtime>

<System. serviceModel>
<Behaviors>
<EndpointBehaviors>
<Behavior name = "imgServiceAspNetAjaxBehavior">
<EnableWebScript/>
</Behavior>
</EndpointBehaviors>
</Behaviors>
<ServiceHostingEnvironment aspNetCompatibilityEnabled = "true">
<BaseAddressPrefixFilters>
<! -- <Add prefix = "http://demo.vs2010.com"/> -->
</BaseAddressPrefixFilters>
</ServiceHostingEnvironment>

<Services>
<Service name = "imgService">
<Endpoint address = "" behaviorConfiguration = "imgServiceAspNetAjaxBehavior"
Binding = "webHttpBinding" contract = "imgService"/>
</Service>
</Services>
</System. serviceModel>
</Configuration>

Okay. Successful! Try uploading to gd Space

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.