1

I have a WCF REST style service using JSON and it works great in IE, but FireFox and Chrome are throwing a tantrum; something about cross domain calls.

I followed the advice here: Problem sending JSON data from JQuery to WCF REST method

But what I am now seeing is that although my browser sends the 2 requests (first for OPTIONS, then for POST), the POST request doesn't return anything at all. Again, IE works perfectly with this. What's missing?

enter image description here

JAVASCRIPT:

<script type="text/javascript">
    function Search() {

        var json = JSON.stringify($('#testForm').serializeObject());

        $.ajax(
        {
            type: "POST",
            url: "http://localhost:8000/MyService.svc/Search",
            data: json,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response, status, xhr) {
                GetResults(response.SearchResult.QueryId);
            },
            error: function (xhr, status, error) {
                alert("Error\n-----\n" + xhr.status + '\n' + xhr.responseText);
            },
            //complete: function (jqXHR, status) { alert('Status: ' + status + '\njqXHR: ' + JSON.stringify(jqXHR)); }
        });

        return false;
    }

    function GetResults(queryId) {
        debugger;
        $.ajax(
        {
            type: "GET",
            url: "http://localhost:8000/MyService.svc/GetResults?queryId=" + queryId + "&ignoreXmlFeedSourceIds=",
            //data: {},
            contentType: 'application/json; charset=utf-8',
            dataType: "json",
            success: function (response, status, xhr) {
                debugger;
                DoSomethingWithResults(response);
            },
            error: function (xhr, status, error) {
                alert(error);
            },
            complete: function (jqXHR, status) { alert('Status: ' + status + '\njqXHR: ' + JSON.stringify(jqXHR)); }
        });
    }

    function DoSomethingWithResults(results) {
        alert("In DoSomethingWithResults()");
        // process the results here to show on UI
        debugger;
    };

    $.fn.serializeObject = function () {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function () {
            if (o[this.name] !== undefined) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    };

</script>

SERVICE CONTRACT:

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    [WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/Search")]
    SearchResponse Search(RequestInfo requestInfo);

    [OperationContract]
    [WebInvoke(Method = "OPTIONS", UriTemplate = "/Search")]
    void SearchAllowCors();

    [OperationContract]
    [WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/GetResults?queryId={queryId}&ignoreXmlFeedSourceIds={ignoreXmlFeedSourceIds}")]
    IList<FlightJourneyAvailabilityResponse> GetResults(string queryId, string ignoreXmlFeedSourceIds);
}
sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Matt
  • 6,787
  • 11
  • 65
  • 112
  • extra comma in AJAX params – Michael B. Aug 30 '13 at 12:03
  • Chrome and Firefox have strict restrictions on CORS. Hence they do not allow calls to cross domain services via jquery. You can achieve cross domain calls via Jquery using JSONP but only GET operations are supported. – Rajesh Aug 30 '13 at 12:51
  • @Rajesh That's completely incorrect. CORS is well supported in Chrome and Firefox. Each of these browsers follow the spec and cross-origin ajax requests are possible and simple using jQuery. – Ray Nicholus Aug 30 '13 at 14:13
  • @RayNicholus : Sorry my mistake that i said they do not allow cross domain requests but i guess under https they block it as it had been my experience and i had to use JSONP. – Rajesh Aug 30 '13 at 14:44

1 Answers1

0

I faced a multitude of problems making WCF work properly with JSON/REST.. so instead I learned about the new ASP.NET Web API which makes this much simpler. For any new web services I will likely be using this instead of WCF.

Matt
  • 6,787
  • 11
  • 65
  • 112