Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C语言中处理服务器上的javascript XMLHttpRequest_Javascript_C_Xml_Http - Fatal编程技术网

在C语言中处理服务器上的javascript XMLHttpRequest

在C语言中处理服务器上的javascript XMLHttpRequest,javascript,c,xml,http,Javascript,C,Xml,Http,我让javascript网页向一个用C编写的小型web服务器发送命令。服务器接收并处理命令没有问题,但当服务器尝试发送响应时,网页似乎没有接收到 下面是javascript代码 function httpGet(command) { var theUrl="http://" + ipAddress + ":" + port + "/command?abcd=abcd&" + command; var xmlHttp = null; xmlHttp = new XM

我让javascript网页向一个用C编写的小型web服务器发送命令。服务器接收并处理命令没有问题,但当服务器尝试发送响应时,网页似乎没有接收到

下面是javascript代码

function httpGet(command)
{
    var theUrl="http://" + ipAddress + ":" + port + "/command?abcd=abcd&" + command;
    var xmlHttp = null;
    xmlHttp = new XMLHttpRequest();
    xmlHttp.open( "GET", theUrl, false ); // need SYNCHRONOUS request
    xmlHttp.send( );
    return xmlHttp.responseText;
}
问题是javascript代码永远不会从上面的send()返回

以下是C语言中的一些服务器代码:

ret =read(fd,buffer,BUFSIZE);

...code to parse out the command from the GET...
...code to process the command

//send the response
sprintf(buffer,"HTTP/1.1 200 OK\nContent-Type: text/xml; charset=utf-8\nContent-Length: 0\nConnection: close\n\n");
write(fd,buffer,strlen(buffer));
sleep(2); // sleep drains the socket
close(fd);
C代码一直运行到完成,但是web浏览器没有收到响应,导致javascript挂起在send()中

服务器没有任何要发送回的数据。它发送的内容长度为0

这是响应javascript XMLHttpRequest()的正确方法吗

谢谢
rough

尝试使用换行符(\r)显式发送回车(\n)


您不需要
sleep()
来“排空插座”。它不像铜管:)试试
xmlHttp.send(“”
)。(顺便说一句,如果您收到XML,您可能希望使用
return xmlHttp.responseXML
)我刚才尝试添加,但服务器仍然获取并处理命令,但浏览器从未获得响应。当我使用浏览器调试时,它甚至没有显示它得到任何字节。请确保缓冲区足够大以容纳数据。是的,缓冲区足够大,谢谢你的评论。但是,我使用了wireshark,数据正在发送到浏览器,但它仍然不会从javascript发送返回()。请尝试使用异步ajax,也可以为xml设置内容类型,但不发送任何内容。@NS您应该发布一个答案,以便任何有类似问题的人都可以找到它作为答案。
sprintf(buffer,"HTTP/1.1 200 OK\r\nContent-Type: text/xml; charset=utf-8\r\nContent-Length: 0\r\nConnection: close\r\n\r\n");