wymsee / cordova-HTTP

Cordova / Phonegap plugin for communicating with HTTP servers. Allows for SSL pinning!
MIT License
371 stars 300 forks source link

Problem making json post request #113

Open ehaab opened 8 years ago

ehaab commented 8 years ago

I tried to use this plugin(both original and forked version(https://github.com/brendonparker/cordova-HTTP)) to send the following information to api server but i don't get any response back ,but for different api url i get fail and success error!The alerts doesn't popup but i see on charle proxy that request has been made correctly with return data but the return data doesn't send back to JavaScript!.Could any one tell me why i don't get fail or success alerts ? Thanks in advance.

data to send via json post request:

POST /api/itemURL/1234 HTTP/1.1 API_KEY: XXXXXXXXXXXXXXXXXXXXXXXX UserID: XXXXXXXXXXXXXXXXXXXXXXXXX User-Agent: Dalvik/1.6.0 (Linux; U; Android 4.2.2; Galaxy Nexus Build/JDQ39) Host: XXXXXXXXXXXXXXXXXXXXX Connection: Keep-Alive Accept-Encoding: gzip Content-Type: application/x-www-form-urlencoded Content-Length: 0

code used to make json post request:

var data = { API_KEY: "XXXXXXXXXXXXXXXXXXXXXXXX", UserID: "XXXXXXXXXXXXXXXXXXXXXXXXX" };
var headers = {"User-Agent": "Dalvik/1.6.0 (Linux; U; Android 4.2.2; Galaxy Nexus Build/JDQ39)", "Host": "XXXXXXXXXXXXXXXXXXXXX", "Connection": "Keep-Alive", "Accept-Encoding": "gzip", "Content-Type": "application/x-www-form-urlencoded", "Content-Length": "0" };

var url = "http://xxxxxxxxxxxxxxx/api/itemURL/1234";
cordovaHTTP.postJson(url,data,headers, { },
function(response) { 
// success
    console.log("HTTP response: "+ response.status);   
console.log(response.data);
    alert("Data: " + response.data + "\nStatus: " + response.status);
      // parse JSON response
    var jsonResponseObj = JSON.parse(response.data);  
}, function(response) {   
// error    
alert("Status: " + response.status);   
console.log("HTTP response: "+ response.status);
}
);
paltefogt commented 7 years ago

It looks like you have an extra parameter in your postJson call. Get rid of that empty object { }

cordovaHTTP.postJson(url, data, headers, function(response) { }, function(error) { });

mburger81 commented 7 years ago

@ehaab Where do you have postJson in this repository?!? This plugin does not support posting json. Right?