Currently I am developing a PhoneGap app and for this I have got a small upload app method created, which will take the base64 string of the picture and send it to the server ( AJAX ). In the browser on my desktop everything goes good, no error and the data gets logged into the db, but when I am trying to do the same from my PhoneGap app something went wrong and I cannot call the methode.
The results of LogCat:
Unknown chromium error: -324
The source:
Client:
document.getElementById('btnReport').addEventListener('click', function (){
api.upload(encodeURIComponent(image), location.coords.accuracy, location.coords.latitude, location.coords.longitude, location.timestamp, function (err, data){
console.log("successful");
alert("Your report was successful!");
});
});
Client side api:
this.upload = function (picture, accuracy, latitude, longitude, timestamp, callback) {
//XMLHttpRequest
var xhr = new XMLHttpRequest();
//recieving response from the server asynchronoues
xhr.open('GET', host.concat("/").concat(API.atoken).concat("/upload/").concat(picture).concat("/").concat(accuracy).concat("/").concat(latitude).concat("/").concat(longitude).concat("/").concat(timestamp).concat("/").concat(API.uid), true);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
//Parse response to a JSON Object
var data = JSON.parse(xhr.responseText);
//If everything went right
if (data.state) {
//Execute callback and pass ranking array
callback(null, {});
}
//Otherwise
else {
callback({
//Methode for getting the reason of failure
'getReason': function (){
//Returns the reason sent from the server why the login request failed
return data.reason;
}
}, null);
}
}
}
xhr.send();
}
Server side:
app.get('/:appid?/:atoken?/upload/:picture?/:accuracy?/:latitude?/:longitude?/:timestamp?/:userid?', function (req, res){
console.log("Upload called");
var picture = tools.escape(req.params.picture, "Picture", res);
var accuracy = tools.escape(req.params.accuracy, "accuracy", res);
var latitude = tools.escape(req.params.latitude, "latitude", res);
var longitude = tools.escape(req.params.longitude, "longitude", res);
var timestamp = tools.escape(req.params.timestamp, "timestamp", res);
var userid = tools.escape(req.params.userid, "Userid", res);
var atoken = tools.escape(req.params.atoken, "Access token", res);
var appid = tools.escape(req.params.appid, "Appid", res);
db.atoken.findOne({"atoken": atoken}, function (err, data){
if (err) { res.send(500, "error"); return;}
//Note: db == Data Base
//If there is a user with this acces token in the db
if (data) {
//Check if the id is the same
//if (data._id == userid) {
//Save all the data in the db
db.entries.save({"accuracy": accuracy, "latitude": latitude, "longitude": longitude, "timestamp": timestamp,"picture": picture, "userid": userid, "reviewed": false, "ip": req.ip, "date": new Date()});
res.send(200, {"state": true});
//}
//If the id is not the same return
//else {
// res.send(400, {"state": false, "reason": "id is not the same"});
//}
}
//If there is no such user in db --> return false
else {
res.send(400, {"state": false, "reason": "No such user in the system"});
}
});
});