Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.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
Javascript nodejsxmlhttprequest响应_Javascript_Ajax_Node.js - Fatal编程技术网

Javascript nodejsxmlhttprequest响应

Javascript nodejsxmlhttprequest响应,javascript,ajax,node.js,Javascript,Ajax,Node.js,我正在制作和应用程序,我需要得到圣经一章的诗句数量。 我从 为了做到这一点,我正在做一个XMLHttpRequest,从站点的函数getVerses()发送到服务器。 我面临的问题是,我没有从XMLHttpRequest获得.responseText。当我使用firebug并调用该函数时,在网络选项卡>响应选项卡中,我什么也得不到,但在网络选项卡>预览中,我得到了答案。 这个答案是从哪里来的?有这个值的变量是什么 我的节点代码如下: let XMLHttpRequest2 = require("

我正在制作和应用程序,我需要得到圣经一章的诗句数量。
我从
为了做到这一点,我正在做一个XMLHttpRequest,从站点的函数
getVerses()
发送到服务器。
我面临的问题是,我没有从XMLHttpRequest获得.responseText。当我使用firebug并调用该函数时,在网络选项卡>响应选项卡中,我什么也得不到,但在网络选项卡>预览中,我得到了答案。
这个答案是从哪里来的?有这个值的变量是什么

我的节点代码如下:

let XMLHttpRequest2 = require("xmlhttprequest").XMLHttpRequest;

function getVerses() {
    let xmlhttp = new XMLHttpRequest2(); //: new ActiveXObject("Microsoft.XMLHTTP");
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == xmlhttp.DONE ) {
            if(xmlhttp.status == 200){
                console.log(xmlhttp.responseText);
            }
            else if(xmlhttp.status == 400) { }
            else { }
            }
    }
    xmlhttp.open("POST", "http://www.kingjamesbibleonline.org/ajax.php", true);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlhttp.send('callFunc=getMaxVerseForChapter&book='+'"Genesis"'+'&chapter='+'"2"');
}

getVerses();

显然,服务器非常严格,它希望标题被称为
Content-Type
,而不是
Content-Type
。显然是一些写得很糟糕的东西(在PHP中)。此外,还要去掉发送的值周围的双引号

给你:

let XMLHttpRequest2 = require("xmlhttprequest").XMLHttpRequest;

function getVerses() {
    let xmlhttp = new XMLHttpRequest2(); //: new ActiveXObject("Microsoft.XMLHTTP");
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == xmlhttp.DONE ) {
            if(xmlhttp.status == 200){
                console.log(xmlhttp.responseText);
            }
            else if(xmlhttp.status == 400) { }
            else { }
        }
    }
    xmlhttp.open("POST", "http://www.kingjamesbibleonline.org/ajax.php", true);
    xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    xmlhttp.send('callFunc=getMaxVerseForChapter&book=' + 'Genesis' + '&chapter=' + '2');
}

getVerses();
由于您正在对值进行硬编码,因此实际上不需要字符串串联:

xmlhttp.send('callFunc=getMaxVerseForChapter&book=Genesis&chapter=2);

没有关系。。。nodejs在CORS
Network tab>Response tab
上不会有问题。。。坚持这是不是nodejs?你的问题是
'callFunc=getMaxVerseForChapter&book='+''Genesis'+'和chapter='+''2'
。。。将其更改为
'callFunc=getMaxVerseForChapter&book='+'Genesis'+'和chapter='+'2'
——换句话说,去掉
关于书籍和章节价值更简单——
'callFunc=getMaxVerseForChapter&book=Genesis&chapter=2'
关于硬编码数据谢谢你的答案@JaromandaX,但它仍然没有打印答案。你太棒了。非常感谢。