Gabriel Mendez
Gabriel Mendez

Reputation: 1115

jQuery AJAX & WCF Exception Failure after POST request

I am trying to post some JSON to my WCF services from an $.ajax query. Here is my set of parameters:

$.ajax(
{
 type: "POST",
 url: theurl,
 data: '{name:"Gabriel"}',
 dataType: "json",
 async:false,
 timeout: 5000,
 //success and error callbacks here...

The WCF method contract looks like:

[OperationContract]
        [WebInvoke(Method = "*", 
                   RequestFormat = WebMessageFormat.Json,
                   BodyStyle = WebMessageBodyStyle.Bare,
                   UriTemplate = "/Sample/POST")]
        string postSample(Stream jsondata);

The WCF is running on a remote server (the cross-domain features and Access-Control-Allow-Origin are enabled there). When I debug using w3wp.exe, I see the WCF methods are hit, the POST body is correct, but, after the service method returns, (an simple JSON or empty string), I'm getting a

[Exception... "Failure"...> :: line 4"  data: no]

on Firefox. Safari and Chrome throws:

Error: NETWORK_ERR: XMLHttpRequest Exception 101

I am using VS2010 and jQuery 1.7.xx

Any idea what I am doing wrong? Any suggestion would be really appreciated.

Upvotes: 3

Views: 644

Answers (1)

Gabriel Mendez
Gabriel Mendez

Reputation: 1115

I found a solution already: I just added the next line at the beginning of the WCF method:

WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*")

and it worked like a charm. Thanks everybody!

Upvotes: 1

Related Questions