奇jQuery的问题 - Ajax请求一个C程序不太工作 [英] Odd jQuery problem - Ajax request to a C program not quite working

查看:104
本文介绍了奇jQuery的问题 - Ajax请求一个C程序不太工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的基本设置:

  • 在C程序打开一个特定的未使用的端口上的插座(假设1234)并等待,听着专门为HTTP请求。​​
  • 网页在同一台机器,主要写的CGI上,用了jQuery做一个AJAX请求针对本地主机:1234

我想对于C程序,看的要求,从它需要一定的数据,并使用包含其自身的一些数据的简单HTTP响应做出回应。它的半工作:该网页看到响应,读取正确的HTTP标头(通过萤火验证这一点),但它没有得到响应数据。打开它在Wireshark的显示,整个响应经历,头和数据一起,但jQuery的似乎只是简单的忽略数据本身。

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.

在jQuery是很简单的:

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.

什么是奇怪的(对我来说,反正)是,如果我打开Firefox和它指向本地主机:1234 ,它加载完全按照我期望 - 的C程序响应的数据表明,我已经没有发送数据;火狐使得它(具体而言,响应数据)就好了。同样的事情发生,如果我telnet到那个端口,手动发送(有点傻傻的看着)的HTTP请求。所以,据我了解,这已成为一些时髦事与jQuery的,但我很茫然,什么可能是!

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!

我试过设置在AJAX请求到各种事物的dataType参数 - 为text / html,text / plain的;我甚至试过XML和修改后的C程序来发送沿。没有良好的 - 他们都做同样的事情。该网页获得正确的HTTP头,但是没有数据。

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.

有也没有错误雨后春笋般冒出来,我可以找到;萤火虫是不是在抱怨; C程序不抱怨......这些非常默默不工作的错误只有一个。

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.

任何想法,乡亲?

编辑:添加实际请求经历的Firefox的要求,

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

成功的请求(如,只是折腾本地主机:6112到地址栏):

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请求头:

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)

推荐答案

在同一网站上不同的端口仍然违反了XHR跨域策略。它可以工作跨域的唯一办法就是要么JSONP(不支持POST请求),或在同一台服务器上的代理web服务。

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是最简单的,它的作用基本上是添加&LT;脚本&GT; 标记的&LT; HEAD&GT; 的URL,这将执行一个GET请求(因此再见POSTDATA)。而且你可以把它通过添加一个调用JavaScript回调回调=? GET参数的URL - 那么响应将不得不调用在回调参数中指定的JavaScript方法。如果这是有道理的。

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的大概检测,这是一个远程URL并尝试使用JSONP,但因为没有回调作出不成功(和错误不会被调用任一)。

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

这篇关于奇jQuery的问题 - Ajax请求一个C程序不太工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆