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

后端 未结 6 1287
灰色年华
灰色年华 2021-01-12 15:00

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:

<         


        
相关标签:
6条回答
  • 2021-01-12 15:16

    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

    0 讨论(0)
  • 2021-01-12 15:16
    function ListProdcutsCtrl($scope, $http) {
        var request = {'searchString' : 'apple'};
        $http.get('/api/products', request).success(function(response) {
            $scope.products_table_data = response.products;
        });
    
    0 讨论(0)
  • 2021-01-12 15:20

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

    0 讨论(0)
  • 2021-01-12 15:25

    There is an alternative in AngularJS called $http, you can find more 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 get JSON data from another domain. However, the data you get should not be plain JSON, but rather a Javascript file like this :

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

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

    0 讨论(0)
  • 2021-01-12 15:33

    JSONP is used to overcome the cross-domain restriction of the 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")
    

    The Syntax for AngularJS JSONP request is :

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

    where url is "string" type representing Relative or absolute URL specifying the destination of the request. The name of the callback should be the string JSON_CALLBACK, and [config] is Optional configuration object.

    0 讨论(0)
  • 2021-01-12 15:38

    You may use JSONP requests with $http.jsonp

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

    0 讨论(0)
提交回复
热议问题