因此,我遇到了一個使用nodejs與API進行通信的問題。我已經嘗試了所有的一半,從使用不同的請求模塊來改變格式和其他所有的一半。Nodejs POST請求不起作用;沒有顯示真正的錯誤代碼
的代碼是在這裏...
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完美的作品在其他計算機上 e返回這個(使用完全相同的Ajax請求的格式,也。):
{ 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
你可以分享服務器端代碼嗎?你也試圖提出你的第一個要求的例子,沒有看到類型設置爲發佈,也沒有包含正文。你的函數是覆蓋請求命名空間的目的是什麼? –
我不能,對不起。另外,是的,我是。看到'type:'POST'。 –
我的意思是這個:function request(name){...} –