JavaScript calls the WCF service and processes the returned dictionary collection

Source: Internet
Author: User

1. How to create a WCF service in the first step

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Runtime.Serialization;
Using System.ServiceModel;
Using System.ServiceModel.Activation;
Using System.ServiceModel.Web;
Using System.Text;

Namespace WcfService1
{
[ServiceContract (Namespace = "Valsun", Name = "Service1")]
[Aspnetcompatibilityrequirements (Requirementsmode = aspnetcompatibilityrequirementsmode.allowed)]
public class HelloWorldService
{
To use HTTP GET, add the [WebGet] attribute. (Default Responseformat is Webmessageformat.json)
To create an operation that returns XML,
Please add [WebGet (Responseformat=webmessageformat.xml)],
and include the following lines in the action body:
WebOperationContext.Current.OutgoingResponse.ContentType = "Text/xml";
[OperationContract]
public string HelloWorld ()
{
Add an action implementation here
Return "You are good ah good";
}

Add more actions here and mark them with [OperationContract]

[OperationContract]
Public dictionary<string, string> gettestlist (string id)
{
Define a Dictionary object
dictionary<string, string> dic = new dictionary<string, string> ();
Easy to test, directly constructs some data to simulate the data in the database
Dic. ADD ("1" + ID, "Shanghai");
Dic. ADD ("2" + ID, "Beijing");
Dic. ADD ("3" + ID, "Guangzhou");
Dic. ADD ("4" + ID, "Shenzhen");
Dic. ADD ("5" + ID, "Nanjing");
return value
return dic;
}
}
}

Second step to create a page call method

<%@ Page language="C #"inherits="system.web.mvc.viewpage<dynamic>"%><! DOCTYPE HTML Public"-//W3C//DTD XHTML 1.0 transitional//en" "HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-TRANSITIONAL.DTD">"http://www.w3.org/1999/xhtml">"Server"> <title>Index</title> <script src="<%= url.content ("~/scripts/jquery-1.4.1. js")%>"type="Text/javascript"></script> "Form1"runat="Server"> <div> <br/> <br/> <input id="btnquerydictionary"Type="Button"Value="Test"onclick="btnquery ();"/> <br/> <br/>message:<p id="lblmsg"></p> </div> <script type="Text/javascript">function Btnquery () {varID ="1"; varWcfproxy =NewValsun.service1 ();    Wcfproxy.gettestlist (ID, onsucceededcallback, onfailedcallback); } function Onsucceededcallback (result, UserContext, methodName) {if(MethodName = ="gettestlist") {            varmsg =""; //pay attention to the way of access here!!!              for(varKeyinchresult) {msg+ = Result[key]. Key +" : "+ Result[key]. Value +"\ n"; }            $("#lblMsg"). HTML (msg); }} function Onfailedcallback (Error, UserContext, methodName) {alert ("Exception Information:"+ error.get_message () +"\ n"+"Exception Type:"+ error.get_exceptiontype () +"\ n"+"Stack information:"+error.get_stacktrace ()); }</script> <asp:scriptmanager id="ScriptManager1"runat="Server"> <Services> <asp:servicereference path="http://localhost:2813/HelloWorldService.svc"/> </Services> </asp:ScriptManager></form></body>View Code

Returned is the dictionary collection, very simple, I misplaced the reason is to write a wrong word, this is implemented with jquery

JavaScript calls the WCF service and processes the returned dictionary collection

Related Article

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.