Is there a method in angular JS equal to getJSON. [Warning for beginners]

I am new to javascript, angularJS and jQuery, but I just started programming an angularJS application, where I use jQuery to get JSON from a web server as follows:

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 I do not need to import the jQuery library.

Thanks in advance, newbie.

This is my decision:

 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 get an error:

Uncaught SyntaxError: Unexpected token:

Why is this? What am I doing wrong? }

+6
source share
6 answers

Due to the help I received 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 with the same problem in the future: D

+8
source

You can use $http to send AJAX requests to Angular.

+2
source

You can use JSONP requests with $ http.jsonp

https://docs.angularjs.org/api/ng/service/$http#jsonp

+2
source
 function ListProdcutsCtrl($scope, $http) { var request = {'searchString' : 'apple'}; $http.get('/api/products', request).success(function(response) { $scope.products_table_data = response.products; }); 
+1
source

AngularJS has an alternative to $http , you can find it here . For instance:

 $http({method: 'JSONP', url: 'http://domain.com/page?json_callback=JSON_CALLBACK'}).success( function(data, status) { // your stuff. } ); 

Or even shorter:

 $http.jsonp('http://domain.com/page?json_callback=JSON_CALLBACK').success( function(data, status) { // your stuff. } ); 

JSONP (JSON Padding) allows you to receive JSON data from another domain. However, the data you receive should not be just JSON, but rather a Javascript file like this:

 JSON_CALLBACK([ {"name": "apple", "color": "red"}, {"name": "banana", "color": "yellow"} ]); 

If your JSON data that you need comes from the same domain, you do not need JSONP.

+1
source

JSONP is used to overcome the cross-domain restriction of AJAX URL calls.

With AngularJS (v1.5), you can use this code to send cross-domain requests:

 $http.jsonp(baseurl+'?token=assume_jwt_token'+encoding+type + "&callback=JSON_CALLBACK") 

AngularJS JSONP request syntax:

 $http.jsonp(url, [config]); 

where url is a string type representing a relative or absolute URL that defines the purpose of the request. The callback name must be a JSON_CALLBACK string, and [config] an optional configuration object.

0
source

Source: https://habr.com/ru/post/955900/


All Articles