orlando marinella orlando marinella - 6 months ago 61
Ajax Question

Nodejs POST request not working; no real error code shown

So, I'm having an issue communicating with an API using nodejs. I've tried everything and a half, from using different request modules from the one supplied to changing the format and everything else and a half.

The code is here...

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);
});


I'm at my wits end. Two different people have no idea what's going on here, and the API works perfectly on another computer(uses the exact same Ajax request format, too.)
e returns this:

{ 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' }


Node Version v4.2.6

Answer

Okay.

So, this wasn't a server issue. I've figured it out, and it's slightly obscure. Note: This is applicable to other nodejs servers using the npm request module to send a json object.

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);
  });

This code above, doesn't work. However, with a little correction,

var mOptions = {
    url: url,
    json: obj,
    processData: false,
    contentType: "application/json",
    type: "POST"
  }
  request.post(mOptions, function(error, response, body){
      console.log(response);
  });

It does. I'm unsure why this makes such a large of a difference, whether it's a server thing or the module's thing, but this is what does it. Ta-da. Thanks for helping.