所以,我在使用nodejs与API通信时遇到问题。我已经尝试了一半,从使用提供的不同的请求模块到改变格式和其他所有的一半。
代码在这里......
var request = require("request");
var prompt = require('prompt');
var $ = require('jquery');
var jsdom = require('jsdom');
var y = [];
var z = [];
var ids = [];
var x = "";
var pTL = "";
var vTL = "";
var url = "";
function requestN(name){
//does things regarding name.
url = https://example.com/ //Not actual domain. Is example.
request(url, function (error, response, body) { //Grabs data from server
if (!error && response.statusCode == 200) {
x = body;
}
if (error || response.statusCode != 200 ){
console.log('Network Error ' + response.statusCode + '. Program will exit shortly...');
}
prepareInput();
});
}
function format(){
//There is a whole lot of things here that regard parsing strings and things like that. This isn't the issue; I checked.
for( var d1 = 0; d1 < ids.length; d1++){
if( d1 + 1 != false){ //Checks if undefined.
var obj = {
"_label": "LABEL",
"_inV": ids[d1],
"_outV": ids[d1 + 1]
}
sendTo(obj);
}
}
};
function sendTo(obj){
jsdom.env("", ["http://code.jquery.com/jquery.min.js"], function(err, window) {
var $ = window.$
$.support.cors = true;
$.ajax({
url: url,
data: JSON.stringify(obj),
processData: false,
contentType: "application/json",
type: 'POST',
error: function(e) {
console.log(e.status); //Always returns a code of '0', for some reason.
console.log(JSON.stringify(obj)) //debug
},
success: function(objString){ //Success never triggers, for some reason.
console.log('Potato'); //debug
console.log(objString);
}
});
});
/* This was also tried; The API kept throwing error 500 at me.
var mOptions = {
url: url,
data: JSON.stringify(obj),
processData: false,
contentType: "application/json",
type: "POST"
}
request.post(mOptions, function(error, response, body){
console.log(response);
});
*/
}
prompt.start(); //Asks for your input
console.log('Please enter property and value to link, and the page to link it to.');
prompt.get(['name', 'property', 'val'], function(err, result){
pTL = result.property;
vTL = result.val;
var name = result.name;
requestN(name);
});
我的智慧结束了。两个不同的人不知道这里发生了什么,并且API在另一台计算机上完美运行(也使用完全相同的Ajax请求格式。) e返回:
{ readyState: 0,
getResponseHeader: [Function],
getAllResponseHeaders: [Function],
setRequestHeader: [Function],
overrideMimeType: [Function],
statusCode: [Function],
abort: [Function],
state: [Function],
always: [Function],
then: [Function],
promise: [Function],
pipe: [Function],
done: [Function],
fail: [Function],
progress: [Function],
complete: [Function],
success: [Function],
error: [Function],
responseText: '',
status: 0,
statusText: 'error' }
节点版本v4.2.6
答案 0 :(得分:-1)
尝试使用 dataType 字段将数据格式化为json。
以下是我用来获取(OAUTH2)令牌的示例:
function doRequestDataWithUserPass(){
var userId="a@b.com";
var pass="aaa";
var formUserPass = { client_id: clientId,
client_secret: clientSecret,
username: userId,
password: pass,
scope: 'read_station',
grant_type: 'password' };
$.ajax({
async: false,
url: "https://example.net/oauth2/token",
type: "POST",
dataType: "json",
data: formUserPass,
success: onSuccess
});
}
function onSuccess(data){
// debug
}
答案 1 :(得分:-1)
好。
所以,这不是服务器问题。我已经弄明白了,而且有点模糊。注意:这适用于使用npm请求模块发送json对象的其他nodejs服务器。
var mOptions = {
url: url,
data: JSON.stringify(obj),
processData: false,
contentType: "application/json",
type: "POST"
}
request.post(mOptions, function(error, response, body){
console.log(response);
});
上面的代码,不起作用。但是,稍作修正,
var mOptions = {
url: url,
json: obj,
processData: false,
contentType: "application/json",
type: "POST"
}
request.post(mOptions, function(error, response, body){
console.log(response);
});
确实如此。我不确定为什么这会产生如此大的差别,无论是服务器还是模块的东西,但这就是它的作用。当当。谢谢你的帮助。