Search code examples
javascriptjqueryangularjssyntax-errorgetjson

Is there a method in angularJS thats equal to getJSON. [Newbie alert]


I'm newbie at javascript, angularJS and JQuery, but I have just started programming a angularJS app where i use JQuery to get a JSON from a webserver like this:

var obj = $.getJSON( "http://something.com/lol?query="+ $scope.searchString, function() {
            $scope.items = obj.responseJSON.entries;                    
        }

Is there a method equal to $.getJSON in angularJS? So that I don't have to import the JQuery library.

Thanks in advance, newbie.

This is my solution so far:

function InstantSearchController($scope, $http){
 $scope.search = function() {   
  $http.jsonp("http://something.com/lol?query="+ $scope.searchString + "?json_callback=JSON_CALLBACK").success(
                        function(data, status) {
                            console.log(data);
                        }
                );
 }

but I'm getting the error msg:

Uncaught SyntaxError: Unexpected token :

why is this? what am I doing wrong? }


Solution

  • Because of the help i got from people answering my question I finally managed to fix it, and i did it like this:

    app.controller('myController', function($scope, $http){
        $scope.items = [];  
         $scope.search = function() {        
                $http({method: 'JSONP', url: "http://something.com/lol?callback=JSON_CALLBACK&query="+ $scope.searchString}).
                  success(function(data, status) {
                    $scope.items = data.entries;
                  }).
                  error(function(data, status) {
                    console.log(data || "Request failed");
                });     
         };
    

    Hope this helps anyone who has the same problem in the future :D