nodejs xhr POST

时间:2016-05-03 02:38:07

标签: javascript node.js xmlhttprequest

我正在使用nodejs并尝试向服务器发出POST命令。我也在使用node-xmlHttpRequest(driverdan' s模块)。我遇到内容类型问题并收到错误:

  

{"响应":{" errorCode":" UNKNOWN_ERROR","消息":"内容类型&#39 ; text / plain的;字符集= UTF-8'不支持","详细信息":"内容类型' text / plain; charset = UTF-8'不支持"},"版本":" 1.0"}

我需要内容类型为JSON,而不是文本。我用GET测试了代码,它运行正常。

这是我的代码:

Int

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

Thanks to jfriend00 I got it working (not sure how to upvote his comment. But here is the code I used:

var apicem_ip = "sandboxapic.cisco.com:9443";
var apic_url = 'https://'+apicem_ip+'/api/v1/ticket';

var request = require('request');

var options = {
  url: 'https://'+apicem_ip+'/api/v1/ticket',
  method: "POST",
  headers: {
    'Content-type': 'application/json'
  },
  body: '{ "username": "admin", "password": "----"}'
};

function callback(error, response, body) {
  console.log("callback function");
  if (!error) {
    var info = (JSON.parse(body));
    console.log(info);
    console.log("status 200");

  }
  else {
    console.log(JSON.parse(body));
  }
}

request.post(options, callback);
相关问题