2016-05-27 81 views
-1

因此,我遇到了一個使用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

+0

你可以分享服務器端代碼嗎?你也試圖提出你的第一個要求的例子,沒有看到類型設置爲發佈,也沒有包含正文。你的函數是覆蓋請求命名空間的目的是什麼? –

+0

我不能,對不起。另外,是的,我是。看到'type:'POST'。 –

+0

我的意思是這個:function request(name){...} –

回答

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

它的確如此。我不確定爲什麼這會造成如此大的差異,無論是服務器還是模塊的事情,但這是什麼。噹噹。感謝您的幫助。

-1

嘗試使用dataType字段將數據格式化爲json。

下面是我用得到(的oauth2)令牌什麼的例子:

function doRequestDataWithUserPass(){ 
    var userId="[email protected]"; 
    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 
}