1

I'm new to angular js, Inside my controller.js are controllers for my app, which is 100% working. What I'm having problem is how to make an ajax call using angular js. I'm trying to fetch data from my service and pass it to my index.html using this ajax. When I try to debug the code, it only hits on $http but doesn't go through the code inside it. What am I doing wrong here?

$http({
        method: 'POST',
        url: 'http://someService.site.net/site.aspx',
        data:{action:'someAction', position:'founders'},
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    }).then(function successCallback(response){

        var json =jQuery.parseJSON(response);
        var htmldata="";
        for(i=0; i<json.length; i++)
        {
            var htmlInfo = '<li><div class=\'col\'><a href="'+ json[i].Id +'"><img width=\'100%\' height=\'auto\' src="'+json[i].Image + '" class=\'profile-img\' /><h3>' +json[i].Name+'</h3><p>'+json[i].Title+'</p></a></div></li>';

            htmldata+= htmlInfo;
        }
        jQuery("#vflist").html(htmldata);
    }, function errorCallback(response){

    });

4 Answers 4

1

Mark your breakpoints on success and error callbacks.

$http is a service. You passed the required data in it's parameter.

$http({})
       ^options

After calling this $http will do it's work and send a request to the provided url asynchronously. But You don't need to debug that part.

$http({options})
    .then(function(data){ 
        // mark 1 breakpoint here
    }, function(data){
        // mark another breakpoint here
    })
Sign up to request clarification or add additional context in comments.

Comments

1

Since you are already using jQuery along with Angularjs I would recommend you to use the $.params() function to encode the parameter data. Your $http would be like

$http({
    method: 'POST',
    url: 'http://someService.site.net/site.aspx',
    data:$.params({action:'someAction', position:'founders'}),
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})

Also I suggest you to use the Angular way of doing things rather than using jQuery. You might even like it ;)

4 Comments

uhmm.. i wanted to do everything using angular js, but I couldn't find answer to my question such as the angular js of parseJSON
You could just go with native JSON.parse() or the Angular's Angular.fromJSON(). Also you could replace $.params with Angular's $httpParamSerializerJQLike link
what difference does it make for my code other than being that as the angular way of parsing json to an object?
Refer this post
1

First thing is: you can simply use your $http function like this:

var config = {
    headers : {
        'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
}};

$http.post('http://someService.site.net/site.aspx',
           {action:'someAction', position:'founders'},
           config)
     .success(function(response) {
        var json =jQuery.parseJSON(response);
        var htmldata="";
        for(i=0; i<json.length; i++) {
            var htmlInfo = '<li><div class=\'col\'><a href="'+ json[i].Id +'"><img width=\'100%\' height=\'auto\' src="'+json[i].Image + '" class=\'profile-img\' /><h3>' +json[i].Name+'</h3><p>'+json[i].Title+'</p></a></div></li>';

            htmldata+= htmlInfo;
        }
        jQuery("#vflist").html($sce.trustAsHtml(htmldata));
     })
     .error(function(data, status, headers, config) {
          console.log(status);
     });

Secondly, as you can see in the above code I have used $sce.trustAsHtml() -- this is required when you are pushing some html to DOM via $http - or it will just show the codes with tags. You have to inject $sce in the controller definition.

5 Comments

it didn't work.. hmm what else am i missing.. but thanks for the information sir @himel
hmm it's hard to say - where the error is occurring without the context.
when I debug the code, it hits at the error, and it shows the config thing Content-Type:"application/x-www-urlencoded;charset=utf-8;"
my bad - there was a semi colon missing after the config variable declaration. Can you check now pls?
hmm it didn't change a thing, it doesn't hit in success nor error :( sorry for being so noob -_-
0

put this line before calling ajax this works for me hope for you to

$http.defaults.headers.common.Authorization = 'Basic YmVlcDpib29w';
// YmVlcDpib29w you can put any string

This is also given in- https://docs.angularjs.org/api/ng/service/$http

1 Comment

the value i'm gonna write for the authorization is that 'Basic YmVlcDpib29w'; also?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.