Calling cross domain WCF service using jQuery

In this post, I will show you how to call a cross-domain service using jquery.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.IO;
using System.ServiceModel.Web;
using System.Runtime.Serialization.Json;

namespace CorssDomainService
    public class Order
        public int ID { get; set; }
        public DateTime OrderDate { get; set; }
        public string Name { get; set; }

    public interface IOrderService
        [WebInvoke(Method = "GET", UriTemplate = "/",
           ResponseFormat = WebMessageFormat.Json
        string ProcessOrder();

    public class OrderService : IOrderService
        public string ProcessOrder()
            Order order = new Order
                ID = 1,
                OrderDate = DateTime.Now,
                Name = "Laptop"


            DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(Order));
            MemoryStream stream = new MemoryStream();
            json.WriteObject(stream, order);
            return Encoding.UTF8.GetString(stream.ToArray());

    class Program
        static void Main(string[] args)
            ServiceHost host = new ServiceHost(typeof(OrderService));
            Console.WriteLine("Service is ready");

<?xml version="1.0"?>
      <service name="CorssDomainService.OrderService" behaviorConfiguration="serviceBehave">
        <endpoint address="rest" bindingConfiguration="crossDomain"  behaviorConfiguration="enableScriptBehaviour"
            <add baseAddress="http://localhost:9090/"/>
        <behavior name="serviceBehave">
          <serviceMetadata httpGetEnabled="true" httpGetUrl="mex"/>
        <behavior name="enableScriptBehaviour">
        <binding name="crossDomain" crossDomainScriptAccessEnabled="true"></binding>

Now, create a website in visual studio, and paste the following code into it

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
    <script type="text/javascript" src=""></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $.getJSON("http://localhost:9090/Rest?callback=?", null, function (data) {

    <form id="form1" runat="server">
Next Post Previous Post
No Comment
Add Comment
comment url