Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.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 节点JS HTTP请求不使用HTTP请求执行任何操作_Javascript_Node.js_Tizen Web App - Fatal编程技术网

Javascript 节点JS HTTP请求不使用HTTP请求执行任何操作

Javascript 节点JS HTTP请求不使用HTTP请求执行任何操作,javascript,node.js,tizen-web-app,Javascript,Node.js,Tizen Web App,我正在尝试使用本机节点js http请求发送http post请求。 我正在使用以下代码,但什么也没发生: var http = require('http'); var options = { hostname: '192.168.1.134', port: '8082', path: '/api', method: 'POST', headers: {'content-type': 'application/jso

我正在尝试使用本机节点js http请求发送http post请求。 我正在使用以下代码,但什么也没发生:

    var http = require('http');

    var options = {
      hostname: '192.168.1.134',
      port: '8082',
      path: '/api',
      method: 'POST',
      headers: {'content-type': 'application/json',
      'cache-control': 'no-cache'}
    };

    callback = function(response)
    {
      var result = [];
      response.on('data', function (chunk)
      {
        result.push(chunk);
      });

      response.on('end', function ()
      {
        console.log("LOCAL END" + result);
      });
    }

    var req = http.request(options, callback);


    req.write(JSON.stringify(
    { 
        customer: 'customer',
        deviceIndicator: 'id',
        userId: 'id2',
        lastVersion: 999 
    }), 'utf8' , 
    function(data)
    {
        console.log('flushed: ' + data);
    });
    req.end();

    console.log(" - trying to post to example - done" );
但是,如果我添加以下虚拟调用,我将从本地服务器获得预期的答案:

    var options1 = {
      hostname: 'www.google.com',
      port: '80',
      path: '/',
      headers: {'cache-control': 'no-cache'}
    };

    callback1 = function(response1) {
      var str = ''
      response1.on('data', function (chunk) {
        str += chunk;
      });

      response1.on('end', function () {
        console.log("GOOGLE END" + str);
      });
    }

    var req1 = http.request(options1, callback1);
    req1.end();

    console.log("sent to google - done");

我做错了什么?

确保可以访问192.168.1.134:8082并进行响应(使用浏览器、curl或wget),然后尝试添加
内容长度
标题:

var http=require('http');
var payload=JSON.stringify({
顾客:“顾客”,
设备指示器:“id”,
用户ID:'id2',
最新版本:999
});
变量选项={
主机名:“192.168.1.134”,
端口:8082,
路径:'/api',
方法:“POST”,
标题:{

“内容长度”:Buffer.bytellength(有效负载),//最终,我发现问题出在设备本身,它有某种问题。。 当将http请求发送到直接ip地址时,什么也没有发生,但当发送到需要dns服务器的地址时,它正在工作


不幸的是,我没有关于这个错误的任何其他信息…

你能添加一个on error块来查看它是否抛出了任何错误吗?尝试在你的POST请求中添加一个
内容长度
标题。192.168.1.134:8082上有什么内容在监听吗?第二个功能正常,因为你正在调用www.google.com:80,它显然会响应,您可能无法在192.168.1.134:8082访问任何内容您是否在
回调中添加了
console.log()
,以确保至少获得响应(在响应正文开始之前)?很抱歉不清楚。我的服务器工作正常。尝试使用Chrome扩展发送请求,效果很好。此外,当向Google服务器发送请求时(在我的请求之前或之后),我会按预期从本地服务器收到响应。服务器可访问且正常工作
var http = require('http');

var payload = JSON.stringify({ 
  customer: 'customer',
  deviceIndicator: 'id',
  userId: 'id2',
  lastVersion: 999 
});

var options = {
  hostname: '192.168.1.134',
  port: 8082,
  path: '/api',
  method: 'POST',
  headers: {
    'content-length': Buffer.byteLength(payload), // <== here
    'content-type': 'application/json',
    'cache-control': 'no-cache'
  }
};

var req = http.request(options, function(response) {
  var result = [];

  response.on('data', function(chunk) {
    result.push(chunk);
  });

  response.on('end', function () {
    console.log('LOCAL END' + result);
  });
});

req.write(payload);
req.end();