Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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? }

like image 602
Erex Avatar asked Oct 14 '13 09:10

Erex


2 Answers

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

like image 141
Erex Avatar answered Sep 27 '22 19:09

Erex


You could use $http to send AJAX requests in Angular.

like image 20
Darin Dimitrov Avatar answered Sep 27 '22 19:09

Darin Dimitrov