从 Node.js 中的 URL 获取 json 时出现错误请求 400
我不断从我在 Node.js 服务器文件中调用的 URL 收到错误请求错误。有什么想法吗?
我也尝试过外部工作 URL,所以这不是本地主机问题。
var http = require('http'),
io = require('socket.io');
server = http.createServer(function(req, res){
res.writeHead(200, {'Content-Type': 'text/html'});
});
server.listen(8181);
// socket.io
var socket = io.listen(server);
socket.on('connection', function(client){
function getMessages() {
http.get({ host: 'localhost', port: 8888, path: 'json.php' }, function(response) {
var data = "";
response.on('data', function(chunk) {
data += chunk;
});
response.on('end', function() {
socket.broadcast(JSON.parse(data));
//console.log(data);
//socket.broadcast('{ "alrt": "YES", "message": "Something is wrong!" }');
//socket.broadcast('hi');
});
});
}
setInterval(getMessages, 1000);
client.on('message', function(){})
client.on('disconnect', function(){});
});
I keep getting a Bad Request error from the URL I'm calling in my node.js server file. any ideas?
I've tried external working URLS too, so it's not a localhost issue.
var http = require('http'),
io = require('socket.io');
server = http.createServer(function(req, res){
res.writeHead(200, {'Content-Type': 'text/html'});
});
server.listen(8181);
// socket.io
var socket = io.listen(server);
socket.on('connection', function(client){
function getMessages() {
http.get({ host: 'localhost', port: 8888, path: 'json.php' }, function(response) {
var data = "";
response.on('data', function(chunk) {
data += chunk;
});
response.on('end', function() {
socket.broadcast(JSON.parse(data));
//console.log(data);
//socket.broadcast('{ "alrt": "YES", "message": "Something is wrong!" }');
//socket.broadcast('hi');
});
});
}
setInterval(getMessages, 1000);
client.on('message', function(){})
client.on('disconnect', function(){});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试在
http.get
选项对象的路径字段中添加斜杠“/
”字符:此外,您还缺少
res.end();
在你的http服务器中:Try adding slash '
/
' character in the path field of yourhttp.get
options object:Also you are missing
res.end();
in your http server: