0

I have this basic setup:

  • C program opens a socket on a specific unused port (let's say 1234) and waits, listening specifically for HTTP requests.
  • Webpage on the same machine, written primarily in CGI, uses jQuery to make an AJAX request targeted at localhost:1234.

What I'd like is for the C program to see the request, take some data from it, and respond with a simple HTTP response containing some data of its own. It's half-working: The webpage sees the response, reads in the correct HTTP headers (verifying this via firebug), but it doesn't get the response data. Opening it up in Wireshark shows that the entire response is going through, headers and data together, but jQuery seems to be just plain ignoring the data itself.

The jQuery is simple enough:

$.ajax({ url: "http://localhost:6112",
              data: {test1: 'testa', test2: 'testb'},
              success: function(d,s,x) {
                     alert("Data: " + d);
                     alert("Status: " + s);
                     alert("Object: " + x);
              },
              error: function(xhr) {
                     alert("Error: " + xhr.status);
              }
});

When I hit the button that triggers that, the request gets made; the "Data: " box pops up blank; Status pops up as 'Success', and an object is created. The 'error' function does not get called.

What's weird (to me, anyway) is that if I open Firefox and point it to localhost:1234, it loads exactly as I'd expect -- the C program responds with data indicating that I'd sent no data; Firefox renders it (specifically, the response data) just fine. Same thing happens if I telnet to that port and manually send a (somewhat silly looking) HTTP request. So, as far as I know, this has to be something funky going on with the jQuery, but I am at a loss as to what it could be!

I've tried setting the dataType parameter in the AJAX request to various things -- text/html, text/plain; I even tried XML and modified the C program to send that along. No good -- they all do the same thing. The webpage gets the correct HTTP headers, but no data.

There are also no errors popping up that I can find; Firebug isn't complaining; the C program isn't complaining ... just one of those "very silently not working" bugs.

Any ideas, folks?


edit: Adding the actual requests going through Firefox, as requested

Successful request (as in, just tossing localhost:6112 into the URL bar):

GET / HTTP/1.1
Host: localhost:6112
User-Agent: (is it weird that I feel weird about posting my User-Agent string?)
Accept: text/html, application/xhtml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.7
Keep-Alive: 115
Connection: keep-alive

Successful response:
HTTP/1.1 200 OK
Host: localhost
Content-Type: text/plain
Content-Length: 55
<html><head></head><body>Done and done.</body></html>

AJAX Request headers:

GET /?test1=testa&test2=testb HTTP/1.1
Host: localhost:6112
User-Agent: (UA string)
Accept: text/plain, */*
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Referer: localhost/cgi-bin/runtime.cgi
Origin: localhost

Resulting response headers:
HTTP/1.1 200 OK
Host: localhost
Content-Type: text/plain
Content-Length: 55
<html><head></head><body>Done and done.</body></html>

(Note: because I'm new, I had to crop some of those hyperlinks)

Yi Jiang
  • 49,435
  • 16
  • 136
  • 136
user462879
  • 187
  • 1
  • 13
  • Can you post the HTTP request that fails and the one that is a success? (firefox live HTTP headers) – Incognito Sep 30 '10 at 14:00
  • Try .get (http://api.jquery.com/jQuery.get/) to retrieve data. This does no data parsing like .ajax. – Ivan Sep 30 '10 at 14:13

1 Answers1

2

A different port on the same website still violates the XHR Cross domain policy. The only way it could work cross-domain is either JSONP (does not support POST requests) or a proxy webservice on the same server.

JSONP is the easiest, what it does is basically just add a <script> tag to your <head> to the url, which would perform a GET request (so bye bye postdata). And you can make it call a javascript callback by adding a "callback=?" GET parameter to the url - the response would then have to call the javascript method named in the 'callback' parameter. If that makes sense..

jQuery is probably detecting that it is a remote URL and tries to use JSONP, but because there are no callbacks made it fails (and error does not get called either).

CharlesLeaf
  • 3,201
  • 19
  • 16
  • If I use JSONP, what's the difference from the server program's standpoint? Can I still use the same HTTP headers, or is there funky JSONP formatting going on? – user462879 Sep 30 '10 at 14:19
  • For the server it shouldn't matter, except that you have to receive all the data via GET instead of POST. And add the callback=? (with the question mark, jQuery will replace it) to the url, then on the server you only have to wrap your JSON response inside the name in callback. For example if jQuery sends callback=myFunc then you respond with myFunc({yourjsonobject}); (and send the right Content-Type header for javascript) – CharlesLeaf Sep 30 '10 at 14:23
  • So for example your URL would be `http://localhost:6112/?test1=testa&test2=testb&callback=?` – CharlesLeaf Sep 30 '10 at 14:24
  • I also see you just posted your headers, perfect example as you can see jQuery is already performing a GET request with the parameters appended to the querystring. – CharlesLeaf Sep 30 '10 at 14:26
  • Good catch. I had this same issue trying to connect to tomcat from apache. – Stefan Kendall Sep 30 '10 at 14:43