jQuery 'get' 没有从 FastCGI 应用程序(nginx Web 服务器)C++接收正确的答案
jQuery 'get' does not receive proper answer from C++ FastCGI application (nginx web server)
我有使用fastcgi与网页交互的c++后端应用程序。网页使用jQuery的"get"方法向该应用程序发送请求,但无法收到正确的答案。所有这些都可以在nginx上工作。
这里是c++应用程序的简化源代码(它使用FastCGI c++库libcgi)。Lib from http://www.fastcgi.com/drupal/node/5)
#include <string>
#include "fcgi_stdio.h"
int main() {
FCGX_Init();
std::string port=":9000";
int listenQueueBacklog = 400;
int listen_socket = FCGX_OpenSocket(port.c_str(), listenQueueBacklog);
FCGX_Request request;
FCGX_InitRequest(&request, listen_socket, 0);
while(FCGX_Accept_r(&request) == 0)
{
FCGX_FPrintF(request.out, "Content-type: text/htmlr nrn<TITLE>fastcgi</TITLE>n<H1>Fastcgi: Hello world.</H1>n");
FCGX_Finish_r(&request);
}
return 0;
}
这是网页的来源。
<html>
<head>
<script type="text/javascript" src="jquery-1.7.min.js"></script>
</head>
<body>
<script type="text/javascript">
$.get("http://localhost:8081", function(data, status, xhr) { alert("Data=" + data + ";Status=" + status); });
</script>
</body>
</html>
这里是nginx配置的一部分
server {
listen 8080;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 8081;
server_name localhost;
location / {
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
在打开网页后,我在c++应用程序中收到请求(在while循环内的断点停止)。但是在执行FCGX_Finish_r之后,我没有看到任何警报,即回调函数没有被调用。(当我使用jQuery 1.4.2而不是1.7时,回调函数被调用,但没有数据,我看到' data =;Status=success'警告窗口,所以问题仍然存在)。
还有一件事-如果我只是打开'http://localhost:8081'在浏览器中,一切都是正确的,我看到'Fastcgi:你好,世界。
如果有人能指出问题所在,我将非常感激。
更新:我编辑了nginx配置(删除了8081的服务器侦听并添加了fastcgi pass到文件夹)
server {
listen 8080;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location /foo {
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
和网页源代码
$.get("http://localhost:8080/foo", function(data, status, xhr) { alert("Data=" + data + ";Status=" + status); });
根据omnosis的建议,一切都开始正常工作!
http://localhost:8081
这会导致错误,因为它从不同的URL调用,虽然这只是端口不同,所以这个请求实际上是一个跨域请求,被浏览器禁用。
你应该看看这些主题:
AJAX响应在c++中无效,但在Apache
和使用apache代理转发的解决方案,如Byron Whitlock建议的:
apache HTTPD扩展帮助
您应该将文件夹转发到端口,而不是localhost:8081
,您应该使用localhost:8080/foo
或其他文件,如localhost:8080/foo.cgi
,或使用子域名,如foo.localhost:8080
我不知道如何配置也许这是好的
- 欧拉项目#8答案是大以获得有效答案
- 首要问题的答案让值班员搞错了
- C++问题:用户认为数字1-100,程序提出问题不超过6次即可得到答案。无法正确
- 我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
- 使用 bfs 解决连接组件问题时得到错误的答案
- 同一对象的"sizeof"的不同答案
- 递归求和任务的错误答案
- 我无法在Visual Studio代码中使用CIN输入答案,它说输入您的年龄,但它说只读文本编辑器如何解决这个问题?
- 为什么C++程序在太大时返回代码而不是答案?
- 不理解为什么代码没有产生所需的答案
- 为什么我在代码厨师的 CMPRSS 问题中得到 WA(错误答案)?
- 我的程序将 26 个字母转换为 ascII 没有显示正确答案
- 请找出我的代码中的错误,它在提交得到错误答案的同时仍然适用于我的所有测试用例
- 任何人都可以弄清楚这段代码如何显示运行错误?它打印无限时间 -1 以及正确答案
- C++ 中 pow() 函数的输出没有给出准确的答案
- 函数从指针 c++ 中获取错误的值并返回错误的答案
- 给出不精确答案的递归Karatsuba算法
- 我的 cout 上有一个奇怪的输出,它把答案放在第一位,然后在我调用它的地方放一个奇怪的输出.我该怎么办?
- 对类的未定义引用,常见答案尚未解决
- C ++分割错误,为什么使用"long long"我没有得到答案?