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

后端 未结 6 1325
灰色年华
灰色年华 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: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.

提交回复
热议问题