G'day friends (I'm American, but G'day just sounds cool)
I just installed the WCF Data Services 5.0 package along with the latest ASP.NET Web API and am having a very annoying issue.. From searching on Google, I'm definitely not the only person who's experienced this problem in general but none of the solutions I have found seem to be working.
My problem is that the host application is not accepting requests that contain the $
character and no matter what I try it won't pass off the request to my JsonpMediaTypeFormatter
. Because of that I have no idea if the formatter would or wouldn't fix the actual problem.
Using fiddler, I can see that the request is being made with an "Accepts
" header of "*/*
" which I imagine isn't going through to my formatter and I can't add "*/*
" to the MediaTypeHeaderValue
because it complains that it's a range.
Please any help or suggestions would be appreciated!
Full Description of Setup
I created a sandbox solution to test with that is composed of two ASP.NET web application projects. The first project is called ClientApplication
and the second is called HostApplication
(I'm sure you can deduce which does what). Each of them are hosted by my local IIS in DIFFERENT web sites and IP addresses. I did as much as I could to create them as if they were completely physically detached without actually needing a server.
HostApplication
Has a simple EDMX file mapping to a simpler database with a few basic tables in it, nothing fancy. I also have my service class as follows:
public class ODataService : DataService<Data.SandboxEntities>
{
public static void InitializeService(DataServiceConfiguration config)
{
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
config.UseVerboseErrors = true;
config.SetEntitySetAccessRule("Customers", EntitySetRights.All);
config.SetEntitySetAccessRule("Employees", EntitySetRights.All);
config.SetEntitySetAccessRule("RandomDatas", EntitySetRights.All);
config.SetEntitySetAccessRule("Schedules", EntitySetRights.All);
config.SetEntitySetAccessRule("Shifts", EntitySetRights.All);
}
}
I'm using the same code for the JsonpMediaTypeFormatter
that is used in a comment by Peter Moberg on the question: JSONP with ASP.NET Web API
Finally, I registered my JsonpMediaTypeFormatter
in the Global.asax
file:
protected void Application_Start(object sender, EventArgs e)
{
var config = GlobalConfiguration.Configuration;
config.Formatters.Insert(0, new JsonpMediaTypeFormatter());
}
Client Application
To bring it all together I have a VERY simple page in the client application with the following code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Service Client Application</title>
<script src="Scripts/jquery-1.7.2.js" type="text/javascript"></script>
<script type="text/javascript">
function makeServiceCall()
{
try
{
/*
$.getJSON('http://10.10.1.7/ODataService.svc/Customers?$format=json&$callback=?',
function (response)
{
$.each(response.d, function (index, value)
{
var div = document.createElement('div');
div.innerHTML = value.ClientName;
$('#result').append(div);
})
});
*/
$.ajax({
type: "POST",
url: "http://10.10.1.7/ODataService.svc/Customers",
dataType: "json",
contentType: "application/json",
success: function (result) { alert("Winning."); },
error: function (result) { alert("Losing..."); }
});
}
catch (err)
{
alert(err);
}
}
</script>
</head>
<body>
<form id="MainForm" runat="server">
<div id="result" style="border:1px solid black;background-color:#E8E8E8;"></div>
<button onclick="makeServiceCall();return false;">Call Service</button>
</form>
</body>
</html>
You might notice that there is a bit of commented javascript in there.. this is because I tried two different ways of making the service call (neither of which worked) and I wanted to be sure to note that to you guys.
Thanks again! Jason