Swift Swift - 1 year ago 101
AngularJS Question

AngularJS http post not working

I have created a service and I using that for my login:

EDIT: Added the 'success' and 'error' code.

EDIT 2: I am developing an iOS mobile application which includes Javascript/AngularJS. So is there a way to view errors as alerts..

.service('Login', function($http, $q, $httpParamSerializerJQLike) {
return {
loginUser: function(ipAdd, name, pw) {

var sendurl = 'http://' + ipAdd + ':8080/loginuser/user.cgi';

var postData = {
'ACTION' : 'login',
'LOGIN' : name,

//what is the mistake here?
return $http({
method : 'POST',
url : sendurl,
data : $httpParamSerializerJQLike(postData),
headers : { 'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function(response) {
var x2js = new X2JS();
var jsonObj = x2js.xml_str2json(response.data);

if (typeof jsonObj === 'object') {
return jsonObj;
} else {
// invalid response
return $q.reject(jsonObj);

}).error(function(response) {
//do error
//comes here when no internet connection is found..
return $q.reject(response.data);


I have also included this in app.js:

var app = angular.module("myApp", ['starter.services'],function($httpProvider){
$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';

My actual url looks like this:

'http://' + ipAdd + ':8080/loginuser/user.cgi?ACTION=login&LOGIN=' + name + '&PASSWORD=' + pw;

I have tried this approach too: http://stackoverflow.com/a/25570077/5876598

My service is not returning anything..
I want to know if I'm doing mistake in my url formation, or while sending data.


Answer Source

I added a deferred promise in my service.

I also changed "success().error()" to ".then(function(data, status, headers, config){}) . Don't know why it didn't work when I used success and error.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download