0

我正在尝试向 Parse 发送发布请求:https ://parse.com/docs/rest#objects-creating 。无法使其正常工作。我在我的程序中使用真实的 App 和 Rest API ID。

var https = require('https');

var options = {
 host: 'api.parse.com',
 port: 443,
 path: '/1/classes/GameScore',
 method: 'POST',
 headers: {
    X-Parse-Application-Id: "",
    X-Parse-REST-API-Key: "",
    Content-Type: "application/json";
}
 data: {
    "score": 1337, "playerName": "Sean Plott", "cheatMode": false
 }
}

};

var req = https.request(options, function(res) {
 console.log("statusCode: ", res.statusCode);
 console.log("headers: ", res.headers);

res.on('data', function(d) {
 process.stdout.write(d);
 });
});
req.end();

req.on('error', function(e) {
 console.error(e);
});

编辑:添加错误响应:

SyntaxError: Unexpected token -
at Module._compile (module.js:406:25)
at Object..js (module.js:417:10)
at Module.load (module.js:343:31)
at Function._load (module.js:302:12)
at Array.<anonymous> (module.js:430:10)
at EventEmitter._tickCallback (node.js:126:26)
4

1 回答 1

1

您不能将-其用作不带引号的对象的键。

所以你需要改变

headers: {
  X-Parse-Application-Id: "",
  X-Parse-REST-API-Key: "",
  Content-Type: "application/json";
}

成为

headers: {
  "X-Parse-Application-Id": "",
  "X-Parse-REST-API-Key": "",
  "Content-Type": "application/json";
}
于 2012-04-05T11:03:27.233 回答