2

If the URL that is to be hit has to be passed variables i.e. API.openweathermap.org/data/2.5/forecast/city?name=[random_city_name]&APPID=[key_value],

then what is better to use ajax or angular js.

If I am using ajax then how am I supposed to pass the variable? I am a newbie in this. So, need your help.

3 Answers 3

4

Your url seems to have request parameters and assuming you are using angular1

For this, you can use

$http({
    method: 'GET',
    url: url,
    headers: {},
    params : {}
})

Put your parameters as a map and $http will take care of creating an url. Refer $http documentation here

Sign up to request clarification or add additional context in comments.

2 Comments

is it possible to implement this using ajax?
Ajax internally is XMLHttpRequest. So is $http. You can use plain AJAX but using angular service would be more preferable. Read the document of $http to better acquaint yourself with it.
1

what is better to use ajax or angular js

You can't compare as AJAX provides a way to communicate (send requests and get responses) with the server asynchronously and AngularJS used AJAX to extends the 2-way data binding.

To accomplish the above situation we can use Angular $http service.

var baseUrl = API.openweathermap.org/data/2.5/forecast/city;
var method = 'GET';
var data = {};
var params = {
   "name":cityName,
   "APPID":key_value
};

$http({
    method: method,
    url: baseUrl,
    params : params,
    data : data
}).then(function mySucces(response) {
    $scope.data = response.data;
}, function myError(response) {
    $scope.data = response.statusText;
});

4 Comments

if I want to access the data through my HTML page,then is the same syntax correct i.e. $scope.data? I am very new to angular and am not familiar with the syntax.Thanks!
Yes you can access it in html using angular expression {{ data }} or using ng-repeat if it is an array ng-repeat="item in data".
I meant if I have an input box in html, where I enter the city and I have to access this city in angular.js. Then what am I supposed to do?
<input type="text" ng-model="cityName">, then to access model value : in controller you can access through $scope.cityName and in html you can directly access using angular expression {{ cityName }}
0
You can use angular $http service and pass your params like below.

var UserInfo = function() {
    $scope.userID = "1111";
    var req ={
        "method":"GET",
        "url": someURL + $scope.userID,
        "withCredentials":true
    };
    $http(req).then(function(response) {
        alert('success');
    }, function(response) {
        alert('error');
    });
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.