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
Node.js xmlhttprequest状态始终为0,并且在responseText中没有响应_Node.js_Ajax_Ionic Framework - Fatal编程技术网

Node.js xmlhttprequest状态始终为0,并且在responseText中没有响应

Node.js xmlhttprequest状态始终为0,并且在responseText中没有响应,node.js,ajax,ionic-framework,Node.js,Ajax,Ionic Framework,我编写了一个简单的node.js服务器和ajax请求,分别用于发送和接收请求。尽管每一次更改都使其无法工作。这是我的node.js服务器代码 var express=require('express'); var server=express(); server.get('/sampleResponse',function(req,res){ if(req.method=="POST"){ console.log('reache`enter code here`d pos

我编写了一个简单的node.js服务器和ajax请求,分别用于发送和接收请求。尽管每一次更改都使其无法工作。这是我的node.js服务器代码

var express=require('express');
var server=express();
server.get('/sampleResponse',function(req,res){
    if(req.method=="POST"){
        console.log('reache`enter code here`d post');
        res.status(200).send('connection successful');
    }else if(req.method=="GET"){
        console.log('reached get');
        res.status(200).send('connection successful');
    }

});
server.listen('8001','127.0.0.1');
//////下面是我的html页面。。。正在本地主机上运行:8100

<html>
    <head>
        <link href="lib/ionic/css/ionic.css" rel="stylesheet">
        <link href="css/style.css" rel="stylesheet">
        <script>
            function submitLoginDetails(){                
                var JSONLoginObj={rollNo:document.getElementById("rollNo").value,
                                password:document.getElementById("password").value};
                ///////////////////////////////////////////////////////////////
                var xhttp = new XMLHttpRequest();
                xhttp.onreadystatechange  = function(){ 
                    var xhrdata = "";
                    if(xhttp.readyState  == 4){
                        if(xhttp.status  == 200) 
                            alert(xhttp.responseText); 
                        else 
                            alert(xhttp.status);
                        }
                };
                xhttp.open("GET", "http://localhost:8001/sampleResponse", false);
                xhttp.send();
            }
        </script>
    </head>
    <body>
        <ion-pane>
            <ion-header-bar class="bar-stable">
                <h1 class="title">Login Page</h1>
            </ion-header-bar>
            <ion-content>
                <form>
                    Roll Number:<br><br>
                    <input type="text" id="rollNo"><br><br>
                    Password:<br><br>
                    <input type="text" id="password"><br><br>
                    <button id="loginButton" onclick="submitLoginDetails();">Login</button>
                </form>
                <p id="demo"></p>
            </ion-content>
        </ion-pane>
    </body>
</html>

when access the same page by clicking on link i get response but no response in xhttp.responseText.

函数submitLoginDetails(){
var JSONLoginObj={rollNo:document.getElementById(“rollNo”).value,
密码:document.getElementById(“密码”).value};
///////////////////////////////////////////////////////////////
var xhttp=newXMLHttpRequest();
xhttp.onreadystatechange=函数(){
var xhrdata=“”;
if(xhttp.readyState==4){
如果(xhttp.status==200)
警报(xhttp.responseText);
其他的
警报(xhttp.状态);
}
};
xhttp.open(“GET”http://localhost:8001/sampleResponse“,假);
xhttp.send();
}
登录页面
卷号:



密码:



登录

当通过单击链接访问同一页面时,我在xhttp.responseText中得到响应,但没有响应。
这是一个跨域问题(CORS),意味着您正试图从当前域之外发出请求(获取资源)。您需要允许设置
Access Control allow Origin
以接受所有请求

您可以通过将以下行添加到.js文件(其中有您的express.js代码)来完成此操作


非常感谢大卫。。。。。。你不知道它对我有多重要。我花了好几个小时才让它工作。你能给我一些关于node.js和web开发的好教程的链接吗(没有html教程)。。。。。。谢谢你advance@VineetJain签出此处列出的资源=>
response.writeHead(200, {
    'Content-Type': 'text/plain',
    'Access-Control-Allow-Origin' : '*'
});