I have a .Net server app (holding WCF REST JSONP service) and a javascript application on client side.
To avoid cross-domain calls from javascript side we use JSONP for communication. To make our life easier with the javascript callbacks from server side we are using JavascriptCallbackBehavior: WCF is wrapping all the JSON responses in the callback function.
[JavascriptCallbackBehavior(UrlParameterName = "$callback")]
[ServiceBehavior(...)]
public class MySvcREST : ISvcREST
{
public bool SvcMethod()
{
throw new WebFaultException<string>("ERROR!", HttpStatusCode.InternalServerError);
}
}
IF I use the code above I will get the following response from my service:
HTTP/1.1 200 OK
Content-Length: 31
Content-Type: application/x-javascript
Server: Microsoft-HTTPAPI/1.0
Date: Fri, 03 Aug 2012 11:43:26 GMT
angular.callbacks._1("ERROR!",500);
EDIT1: Actually even if I remove the JavascriptCallbackBehavior and leave the default callback name, I would have similar effect, which is not very good.
I really want to get the HTTP 500 error code, not a HTTP 200 OK with the error inside.
How to solve my problem or is there another way to get JSONP from a WCF service?
Unfortunately I didn’t get any reply to my question, but I found the following, somewhat useful, link: http://msdn.microsoft.com/en-us/library/ee816916.aspx .
I understood that this behavior is by design… although from javascript side (jquery / angular js), at this time of writing, there is no way to read the second returned parameter (in my case, the http error code).
So, in the end I am not sure if there is a solution other than a workaround. For now we return a
CustomResultin which we include a custom error code and message.It is so disappointing with these web technologies when they don’t blend with each other… you are forced to so many workarounds.