开发者

Odd jQuery problem - Ajax request to a C program not quite working

开发者 https://www.devze.com 2023-01-18 10:04 出处:网络
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.

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)


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号