0
接收的一些數據,當我運行這段代碼我無法發送和接收數據。我要求改正我的代碼 這是服務器端代碼 錯誤:從HTML頁面數據發送到服務器和從服務器
var http = require("http") ;
var fs = require("fs") ;
http.createServer(function(req,res){
if(req.url == '/'){
fs.readFile('post.html',function(err,data){
res.writeHead(200, {'Content-Type': 'text/html','Content-Length':data.length});
res.write(data);
res.end();
});
}
else if (req.url == '/dat') {
req.on('data', function (data){
console.log(data.toString());
console.log("yo");
});
console.log("second");
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write("Hi, Server is still alive and awaiting your orders Dear User :)");
res.end() ;
}
else{
console.log("third");
}
}).listen(2000);
這是客戶端代碼
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.post("http://localhost:2000/dat"),
JSON.stringify({
name: "Donald Duck",
city: "Duckburg"
}),
function(data,status){
alert("Data: " + data + "\nStatus: " + status);
}
});
});
</script>
</head>
<body>
<button>Send an HTTP POST request to a page and get the result back</button>
</body>
</html>
我將這兩個代碼保存在同一個文件夾中,並試圖編譯。我成功地運行了這段代碼,但是在對代碼做了一些小修改之後。代碼停止工作 ,我無法撤消該操作。如果可能,我需要更正我的代碼。
實際上你需要什麼作爲迴應?你現在得到什麼? –