Trying to translate a Curl command into Angular JS code has been a challenge for me as a newcomer to AngularJS. This specific instance involves a mobile app built on the ionic framework, which utilizes Angular JS.
The original curl command looks like this:
curl -X POST -d "username=xxxx&password=xxxx&action=login&view=console" http://myserver.com/zm/index.php?skin=xml
It functions perfectly when run from the command line.
Within my AngularJS project, I've written the following code:
angular.module('starter.controllers').controller('MonitorCtrl', function($ionicPlatform, $scope,$http)
{
console.log("***MAKING REQUEST");
//$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
$http({
url:'http://myserver.com:9898/zm/index.php?skin=xml',
method:'post',
headers: {'Content-Type': 'application/x-www-form-urlencoded',
'Accept': '*/*',
},
transformRequest: function(obj) {
var str = [];
for(var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
var foo= str.join("&");
console.log ("****RETURNING "+foo);
return foo;
},
data: {username:'admin',
password:'xxxx',
action:'login',
view:'console'}
})
.success (function()
{console.log("****YAY");
})
.error(function(data, status, headers, config)
{
console.log("***OOPS "+status + " H: "+data);
});
});
While observing the network traffic with httpry, I can see the POST request being sent to the server and receiving a 200OK response with the expected payload, but strangely my success handler is not triggered. Instead, the error handler fires with a status of 0.