Use JavaScript to call the WCF Service and javascriptwcf

Source: Internet
Author: User

Use JavaScript to call the WCF Service and javascriptwcf

 

Address: http://www.cnblogs.com/jfzhu/p/4039604.html

Reprinted please indicate the source

 

We have previously introduced Step by Step to create a WCF Service and use the Trace and Message Log functions of WCF. This article describes how to use JavaScript to call the WCF Service.

The code for the WCF Service is as follows:

IHelloService. cs

using System.ServiceModel;using System.ServiceModel.Activation;using System.ServiceModel.Web;namespace HelloService{        [ServiceContract(Name = "IHelloService")]        public interface IHelloService    {        [OperationContract(Name="GetMessage")]                string GetMessage(string name);        [OperationContract]        Employee GetEmployee(int id);    }}

 

HelloService. cs

using System;namespace HelloService{        public class HelloService : IHelloService    {        public string GetMessage(string name)        {            return "Hello " + name;                    }        public Employee GetEmployee(int id)        {            return new Employee() { Id = id, Name="Neil Klugman", Birthdate=new DateTime(1930, 1, 31)};        }    }}

 

In the web. config file, pay attention to the highlighted part:

 

<?xml version="1.0" encoding="utf-8" ?><configuration>  <system.serviceModel>    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />        <services>      <service name="HelloService.HelloService" behaviorConfiguration="metaBehavior">        <endpoint address="" binding="webHttpBinding" contract="HelloService.IHelloService" behaviorConfiguration="ajaxServiceBehavior"></endpoint>        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>        

 

Create a client web application and add a web form, WebForm1.aspx:

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="HelloWebClient.WebForm1" %><!DOCTYPE html>

 

Open WebForm1.aspx in a browser and use Fiddler to view it, because the Code contains a reference to the WCF Service.

<Asp: ServiceReference Path = "http: // 192.168.6.47: 8080/HelloService. svc"/>

So the page loads JavaScript

 

Type. registerNamespace ('tempuri. org '); tempuri.org. IHelloService = function () {tempuri.org. IHelloService. initializeBase (this); this. _ timeout = 0; this. _ userContext = null; this. _ succeeded = null; this. _ failed = null;} tempuri.org. IHelloService. prototype = {_ get_path: function () {var p = this. get_path (); if (p) return p; else return tempuri.org. IHelloService. _ staticInstance. get_path () ;}, GetMessage: function (name, succeededCallback, failedCallback, userContext) {// <param name = "name" type = "String"> System. string </param> /// <param name = "succeededCallback" type = "Function" optional = "true" mayBeNull = "true"> </param> /// <param name = "failedCallback" type = "Function" optional = "true" mayBeNull = "true"> </param> // <param name = "userContext" optional = "true" mayBeNull = "true"> </param> return this. _ invoke (this. _ get_path (), 'getmessage', false, {name: name}, succeededCallback, failedCallback, userContext) ;}, GetEmployee: function (id, succeededCallback, failedCallback, userContext) {// <param name = "id" type = "Number"> System. int32 </param> /// <param name = "succeededCallback" type = "Function" optional = "true" mayBeNull = "true"> </param> /// <param name = "failedCallback" type = "Function" optional = "true" mayBeNull = "true"> </param> // <param name = "userContext" optional = "true" mayBeNull = "true"> </param> return this. _ invoke (this. _ get_path (), 'getemployee ', false, {id: id}, succeededCallback, failedCallback, userContext) ;}} tempuri.org. IHelloService. registerClass ('tempuri. org. IHelloService ', Sys. net. webServiceProxy); tempuri.org. IHelloService. _ staticInstance = new tempuri.org. IHelloService (); tempuri.org. IHelloService. set_path = function (value) {tempuri.org. IHelloService. _ staticInstance. set_path (value);} tempuri.org. IHelloService. get_path = function () {// <value type = "String" mayBeNull = "true"> The service url. </value> return tempuri.org. IHelloService. _ staticInstance. get_path ();} tempuri.org. IHelloService. set_timeout = function (value) {tempuri.org. IHelloService. _ staticInstance. set_timeout (value);} tempuri.org. IHelloService. get_timeout = function () {// <value type = "Number"> The service timeout. </value> return tempuri.org. IHelloService. _ staticInstance. get_timeout ();} tempuri.org. IHelloService. set_defaultUserContext = function (value) {tempuri.org. IHelloService. _ staticInstance. set_defaultUserContext (value);} tempuri.org. IHelloService. get_defauserusercontext = function () {// <value mayBeNull = "true"> The service default user context. </value> return tempuri.org. IHelloService. _ staticInstance. get_defauserusercontext ();} tempuri.org. IHelloService. set_defaultSucceededCallback = function (value) {tempuri.org. IHelloService. _ staticInstance. set_defaultSucceededCallback (value);} tempuri.org. IHelloService. get_defaultSucceededCallback = function () {// <value type = "Function" mayBeNull = "true"> The service default succeeded callback. </value> return tempuri.org. IHelloService. _ staticInstance. get_defaultSucceededCallback ();} tempuri.org. IHelloService. set_defaultFailedCallback = function (value) {tempuri.org. IHelloService. _ staticInstance. set_defaultFailedCallback (value);} tempuri.org. IHelloService. get_defaultFailedCallback = function () {// <value type = "Function" mayBeNull = "true"> The service default failed callback. </value> return tempuri.org. IHelloService. _ staticInstance. get_defaultFailedCallback ();} tempuri.org. IHelloService. set_enableJsonp = function (value) {tempuri.org. IHelloService. _ staticInstance. set_enableJsonp (value);} tempuri.org. IHelloService. get_enableJsonp = function () {// <value type = "Boolean"> Specifies whether the service supports JSONP for cross domain calling. </value> return tempuri.org. IHelloService. _ staticInstance. get_enableJsonp ();} tempuri.org. IHelloService. set_jsonpCallbackParameter = function (value) {tempuri.org. IHelloService. _ staticInstance. set_jsonpCallbackParameter (value);} tempuri.org. IHelloService. get_jsonpCallbackParameter = function () {// <value type = "String"> Specifies the parameter name that contains the callback function name for a JSONP request. </value> return tempuri.org. IHelloService. _ staticInstance. get_jsonpCallbackParameter ();} tempuri.org. IHelloService. set_path (" http://192.168.6.47:8080/HelloService.svc "); Tempuri.org. IHelloService. getMessage = function (name, onSuccess, onFailed, userContext) {// <param name = "name" type = "String"> System. string </param> /// <param name = "succeededCallback" type = "Function" optional = "true" mayBeNull = "true"> </param> /// <param name = "failedCallback" type = "Function" optional = "true" mayBeNull = "true"> </param> // <param name = "userContext" optional = "true" mayBeNull = "true"> </param> tempuri.org. IHelloService. _ staticInstance. getMessage (name, onSuccess, onFailed, userContext);} tempuri.org. IHelloService. getEmployee = function (id, onSuccess, onFailed, userContext) {// <param name = "id" type = "Number"> System. int32 </param> /// <param name = "succeededCallback" type = "Function" optional = "true" mayBeNull = "true"> </param> /// <param name = "failedCallback" type = "Function" optional = "true" mayBeNull = "true"> </param> // <param name = "userContext" optional = "true" mayBeNull = "true"> </param> tempuri.org. IHelloService. _ staticInstance. getEmployee (id, onSuccess, onFailed, userContext);} var gtc = Sys. net. webServiceProxy. _ generateTypedConstructor; Type. registerNamespace ('jfzhu. com. _ 2014. _ 10. _ 16. employee '); if (typeof (jfzhu.com. _ 2014. _ 10. _ 16. employee. employee) = 'undefined') {jfzhu.com. _ 2014. _ 10. _ 16. employee. employee = gtc ("Employee: http://jfzhu.com/2014/10/16/Employee "); Jfzhu.com. _ 2014. _ 10. _ 16. employee. employee. registerClass ('jfzhu. com. _ 2014. _ 10. _ 16. employee. employee ');}View Code

 

Click Get Value:

 

 

Use Microsoft Service Trace Viewer to view Message Log Trace:

 

 

 

 

Summary: Use webHttpBinding for binding the endpoint in the configuration file of the WCF Service, and set endpointBehavior to enableWebScript.

In WebForm, you must add a reference to the WCF Service in ScriptManager.


How to Use js to call the wcf Service

What is your public url? What format is returned?
If you have identified these two problems, you can do anything else.

WCF Service call

Configure the Timeout time. In the WEB. CONFIG file, set bindings under the system. serviceModel node and modify the attributes related to Timeout. Here, you must modify receiveTimeout.

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.