cocoaxmlhttprequestgcdwebserver

GCDWebServer returns HTTPRequest status zero with no responseText


I have the following javascript app that receives data from GCDWebServer that runs on an OSX app.

var HttpClient = function() {
        this.get = function(aUrl, aCallback) {
            var anHttpRequest = new XMLHttpRequest();
            anHttpRequest.open( "GET", aUrl, true );   
            anHttpRequest.onreadystatechange = function() { 
                if (anHttpRequest.readyState == 4 && anHttpRequest.status == 200) {
                    aCallback(anHttpRequest.responseText);
                    document.write (anHttpRequest.responseText);
                   }
                   else {
                    document.write ("Not Ok: ReadyState=" + anHttpRequest.readyState + " Status= " + anHttpRequest.status);
                   }
            }      
            anHttpRequest.send( null );
        }
    }

    var client = new HttpClient();
    client.get('http://xxx.xxx.x.xx:8080?var=param', function(response) {
        document.write ("Success"); 
    });

The Cocoa app runs the following code:

[webServer addDefaultHandlerForMethod:@"GET"
                             requestClass:[GCDWebServerRequest class]
                        asyncProcessBlock:^(GCDWebServerRequest* request, GCDWebServerCompletionBlock completionBlock) {

                            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                                GCDWebServerDataResponse* response = [GCDWebServerDataResponse responseWithJSONObject:dict];
                                completionBlock(response);
                            });

I always get an anHttpRequest.readyState = 4 and anHttpRequest.status = 0. The anHttpRequest.responseText is empty.

However, when I open any browser and type http://xxx.xxx.x.xx:8080?var=param I get the correct results. So the server is sending the data properly.

Here is the log from GCDWebServer

[DEBUG] Did open connection on socket 13
[DEBUG] Did connect
[DEBUG] Connection received 312 bytes on socket 13
[DEBUG] Connection on socket 13 preflighting request "GET /" with 312 bytes body
[DEBUG] Connection on socket 13 processing request "GET /" with 312 bytes body
[DEBUG] Connection sent 175 bytes on socket 13
[DEBUG] Connection sent 107 bytes on socket 13
[DEBUG] Did close connection on socket 13
[VERBOSE] [xxx.xxx.x.xx:8080] xxx.xxx.x.xx:52401 200 "GET /" (312 | 282)
[DEBUG] Did disconnect

Any ideas? Thanks in advance.


Solution

  • This isn't an issue with your GCDWebServer but is simply a CORS issue. If you make the same request against your server from something like curl then you will see the correct 200 HTTP status. To get the correct response from a browser you will need to have your server send the following header:

    Access-Control-Allow-Origin: *
    

    This tells the browser that it is allowed to pass back the full response it gets from your server to the JavaScript initiating the request. Without that response header the browser essentially drops all the data it received and should print out an error message in your browser's console.