Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8149577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:52:51+00:00 2026-06-06T14:52:51+00:00

I think I’ve read every StackOverflow post on XDomainRequest and another few dozen on

  • 0

I think I’ve read every StackOverflow post on XDomainRequest and another few dozen on AJAX and WCF, but I’m still having trouble getting an XDomainRequest AJAX call to work. I’ve implemented CORS (“Access-Control-Allow-Origin”) on my WCF service and my code works fine with xmlHttpRequest in Chrome and Firefox, but I’m making calls cross-domain and so for IE I need to use the XDomainRequest object. My xdr works fine when I GET or POST to a method that has no args, and I can even use the GET verb successfully to a method with args using a querystring, but when I try to POST to a method with args my xdr throws an error, even though I put a breakpoint in the BeginRequest method and I see that the Response from the server is “200 OK”. I’d like to think I’ve tried every combination of config file settings but I have to be missing something. Any help in pointing me in the right direction is greatly appreciated.

Here are the pertinent parts of my code:

WCF – Global.asax

protected void Application_BeginRequest(object sender, EventArgs e)
    {
        //for CORS
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
    }

WCF – IService1.cs

[ServiceContract]
public interface IService1
{
    [OperationContract]
    [WebInvoke(Method = "POST")]
    string GetData();

    [OperationContract]
    [WebInvoke(Method = "POST")]
    string GetData2(string param);
}

WCF – Service1.svc

public class Service1 : IService1
{
    public string GetData()
    {
        return "Hello";
    }

    public string GetData2(string param)
    {
        return string.Format("Hello - {0}", param);

    }
}

WCF – Web.config

<?xml version="1.0"?>

<system.web>
    <compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    <services>
        <service behaviorConfiguration="WcfService1.Service1Behavior" name="WcfService1.Service1">
            <endpoint address="AjaxEndpoint" behaviorConfiguration="AjaxBehavior" contract="WcfService1.IService1" bindingConfiguration="AjaxBinding"/> 
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="WcfService1.Service1Behavior">
                <!--<serviceMetadata httpGetEnabled="true" />-->
                <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
        </serviceBehaviors>
        <endpointBehaviors>
            <behavior name="AjaxBehavior">
                <enableWebScript/>
            </behavior>
        </endpointBehaviors>
    </behaviors>
    <bindings>
        <webHttpBinding>
            <binding name="AjaxBinding"/>
        </webHttpBinding>
    </bindings>
</system.serviceModel>
<system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>

Client AJAX call

var WcfURL = "http://localhost/WcfService1/Service1.svc/AjaxEndpoint"
if (window.XDomainRequest) {
//IE - use cross-domain request
xdr = new XDomainRequest();
xdr.onprogress = function () { alert("onprogress: " + xdr.responseText) };
xdr.onload = function () { updateText(xdr.responseText) }; 
xdr.onerror = function () { alert("xdr error") };
xdr.timeout = 7500;
xdr.ontimeout = function () { alert("xdr timeout") };

var data = "passedInParam";
//var method = "/GetData";  //this works
var method = "/GetData2";  //this throws an error
xdr.open("POST", WcfURL + method);

xdr.send(data);

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T14:52:52+00:00Added an answer on June 6, 2026 at 2:52 pm

    I’ve found the solution. Thanks to Fiddler, I was able to view more of the response I was getting back from the service. The error was

    The incoming message has an unexpected message format ‘Raw’. The
    expected message formats for the operation are ‘Xml’, ‘Json’. This can
    be because a WebContentTypeMapper has not been configured on the
    binding.

    Armed with this information I started looking into the WebContentTypeMapper. I found this article useful, and after adding the WebContentTypeMapper method I was able to see that the contentType of my request coming from the XDomainRequest was of type “application/json” (as expected) when I did not include an argument in the XDomainRequest.send() method, but changed to type “application/octet-stream” when an argument was passed in. (ie. xdr.send(data)) I don’t know why it changes to octet-stream, but in doing so it would cause the service method to throw a 500 error (with the message from above) and therefore cause the xdr request to error. But the WebContentTypeMapper is aptly named and using it to change the contentType is easy enough. After correcting it to type Json my xdr is working well.

    Here’s the method:

    public class CustomContentTypeMapper : WebContentTypeMapper
    {
        public override WebContentFormat GetMessageFormatForContentType(string contentType)
        {
            if (contentType == "application/octet-stream")
            {
                return WebContentFormat.Json;
            }
            else
            {
                return WebContentFormat.Default;
            }
        }
    }
    

    And here are the portions of the config file that were updated:

    <endpoint address="AjaxEndpoint" behaviorConfiguration="AjaxBehaviour" contract="WcfService1.IService1" binding="customBinding" bindingConfiguration="CustomMapper"/>
    ...
    <bindings>
            <customBinding>
                <binding name="CustomMapper">
                    <webMessageEncoding webContentTypeMapperType="WcfService1.CustomContentTypeMapper, WcfService1" />
                    <httpTransport manualAddressing="true" />
                </binding>
            </customBinding>
        </bindings>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think that asking this might be kind of silly but I'm still wondering
I think it will be very hard for you to read the code but
I think I have a basic understanding of this, but am hoping that someone
I think this could be a very easy question for you. But I have
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Think of this: A view controller's view is added as subview to another view.
I think this has probably been asked, but after reading a lot, I'm not
I think my question revolves around me not having a comfortable grasp of page
I think under windows x64, it still uses user32.dll and a bunch of other
Think I read a bazillion articles over the past couple of days and yet

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.