Getting 400 Bad Request now that WCF Data Service on separate web server

Apr 5, 2012 at 6:07 PM


I've created an application using Entity Framework, WCF Data Services, and jquery with knockout and datajs.  Originally the Entity models and WCF Data services were in the same project.  When I would run things on my local machine, everything worked fine.  I moved everything over to our development web server, and I noticed immediate problems with the WCF Data Services.  So, I put the entity models and WCF data services into their own project, and I have them running successfully on the development web server.  However, I still cannot get it to work with my client page.  I created a simple test.htm document, stripped out any extraneous things (knockout, jquery-ui, etc.) so that I could test what is going on.  When I inspect the HTTP requests, I am still getting "400 Bad Request" with all of my requests to the web-service.

Here is my page source:

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

<html xmlns="">
    <script type="text/javascript" src="Scripts/jquery-1.4.2.min.js"></script>
    <script type="text/javascript" src=""></script>
    <script type="text/javascript" src="Scripts/datajs-1.0.2.min.js"></script>

<script language="javascript" type="text/javascript">
    var serviceUrl = "http://myserver:8082/Services/MyService.svc/";
    OData.defaultHttpClient.enableJsonpCallback = true;
    OData.jsonHandler.recognizeDates = true;

    var employeeID = 99999;
    var url = this.serviceUrl + "Employee?$filter=EmployeeID eq " + employeeID;
    document.write("Hello");, function(data) {
    function(err) { alert(err.message); });  

If I use my browser to access the webservice with these parameters, I can access it with no problem.  http://myserver:8082/Services/MyService.svc/Employee?$filter=EmployeeID eq 99999 works as expected.  No problems.
However, when I run this page (above), it won't work.  When use Firefox FireBug to inspect the request, it says the URL is:

And the Status is "400 Bad Request".  The Response says:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<error xmlns="">
  <message xml:lang="en-US">The query parameter '$callback' begins with a system-reserved '$' character but is not recognized.</message>
Is the $callback=parent.handlJSONP_0 supposed to be there?  Any ideas as to why this isn't working?


Apr 5, 2012 at 6:55 PM

By the way, my web-service code looks like this:

using System;
using System.Collections.Generic;
using System.Data.Services;
using System.Data.Services.Common;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Web;
using Web.EntityModels;

namespace Web.Services
    [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    public class MyService : DataService<MyEntities>
        // This method is called only once to initialize service-wide policies.
        public static void InitializeService(DataServiceConfiguration config)
            config.SetEntitySetAccessRule("*", EntitySetRights.All);
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;

The web.config in the project with the web-services looks like this (I obviously excluded the connectionstring):

<?xml version="1.0" encoding="utf-8"?>
    <compilation debug="true" targetFramework="4.0">
        <add assembly="System.Data.Entity, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    <add name="MyEntities" connectionString="..." providerName="System.Data.EntityClient" />
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />

Apr 6, 2012 at 3:06 AM
Edited Apr 6, 2012 at 5:27 AM

Hi Tim,

Your webapp is being served through port 80 right? The fact that your data service is at a different port (8082) makes the request to it a cross domain request. Because of this, datajs is trying to use JSONP to make the request.  Out of the box, JSONP is not supported by WCF data services. In order to enable JSONP you can use this extension with your service.


Alex Trigo.

Apr 6, 2012 at 3:20 PM
Edited Apr 6, 2012 at 3:25 PM

Hi Alex,

Thank you for your help.  I actually found that very same extension right around the time I received your reply.  That does fix things.  The only downside is that I I also found that I cannot perform update operations on cross domain requests.  I can see why, for CRUD operations, one would want the WCF Data service to be part of the same project as the HTML page or web form.  I'm fairly new to a lot of this, so perhaps you know the answer to two follow-up questions for me:

1) I had similar trouble while having the WCF Data service project running in the same IS web-site (not a different port) but only with a different virtual directory than the front-end HTML application.  Is that considered "cross domain" as well?

2) Is there a web-service solution out there that works with Entity Framework and would work well with cross domain requests, including update operations, and would also work with datajs?

Thanks again for your help


Apr 27, 2012 at 12:50 AM

Hi Tim,

   Sorry for the delayed response.  In answer to your questions:

   1. Yes, a different port is a different domain and so considered a cross-domain request.

   2. I don't quite understand the question. I don't thinkg EF has anything to do with the issue here.. the problem is completely cross domain.. what you cand o is have a "dummy" web service or IIS handler that serves as proxy for your service in the other domain.  It would only relay the request made by datajs to the desired endpoint in the other domain and then the response back.


Alex Trigo.