Angularjs: Use the json file based on user selection - angularjs

I have multiple json files as my datasource. How to use $http.get() to get the correct json files based on the user selection ?

try something like this
// Simple GET request example:
var fileName = 'myFile.txt' //name coming from where you call this service
$http({
method: 'GET',
url: '/someUrl/'+fileName
}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
the alternative if you are using an api
var fileName = 'myFile.txt'
$http({
url: url: '/someUrl',
method: "GET",
params: {fileName: fileName}
});

Related

How do I fetch json data in angularjs controller

https://jsonplaceholder.typicode.com/posts I have to fetch data from this link and display it in my browser. In Javascript I used to do it using XMLHttpRequest and then parse it using JSON.parse(response). Is there a way I can achieve something similar in angularjs. Thank you
You can review this link angular http get
sample
$http({
method: 'GET',
url: '/someUrl'
}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
}, function errorCallback(response) {
/ / called asynchronously if an error occurs
// or server returns response with an error status.
});

Angular Parse GET Return

m gets generated in a factory with the following request:
var m = $http({method: 'GET', url: JSONurl});
Console log of m after the GET request:
I need to grab m's "data:" which has the Array[2] I need. How would I create a new variable with just the data array?
If you look at the angularJS docs for $http, you'll see that you'll need to use the promise to get the data. So you want something along the lines of:
$http({
method: 'GET',
url: JSONurl
}).then(function successCallback(response) {
//response has the data on a successful call
}, function errorCallback(response) {
//this response will have the error data on a failed call
});

How can i Prevent caching in angular?

I want to prevent caching in angular.For that i set the cache property to
false.After doing this i request the same url .But didn't send that request
to my server.
Used code for preventing,
$http({
cache : false,
method: "GET",
url :"myurl";
}).success(function(data) {
}).error(function(data) {
});
And code used for remove cache,
var $httpDefaultCache = $cacheFactory.get('$http');
$httpDefaultCache.remove('myurl');
$http({
cache : false,
method: "GET",
url :"myurl";
}).success(function(data) {
}).error(function(data) {
});
can u help me?Please
You could pass the dummy parameter in the URL so that URL become an unique by adding data into it. Passing dummy parameter in params array will not harm the $http get call.
$http({
method: 'GET',
url: 'myurl',
params: { 'dummy': new Date().getTime() }
})
This will ensure caching will not be done for your url.
Best option would be disable caching on server side link here

Update $scope.variable value after POST

I am creating an simple TODO app using AngularJS, i POST the data to server when response comes, that response i want to store it existing variable and refresh the view. i.e
// This stores on page load, its working fine
var todos = $scope.todos = sever_passed_data;
but when i do,
$scope.$watch('todos', function () {
var request = $http({
method: "post",
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
url: my_url,
data: $.param({todos_list: angular.toJson(todos)})
});
request.success(function(responce){
var todos = $scope.todos = responce;
});
}, true);
after this it gives me weird(it goes in infinite loop and posting data to server) output, i mean the responce doesn't stores in todos variable.
If you want to store the returned value of the HTTP POST in the $scope.todos variable, you should use response.data. response contains the entire HTTP response, including the response code, response headers, response body, etc.
Why are you declaring the local variable todos? It will go out of scope as soon as the function exits. just assign $scope.todos. Also, you might want to use then instead of success. Here's an example:
$http({
method: "POST",
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
url: my_url,
data: $.param({todos_list: angular.toJson(todos)})
}).then(function(response) {
$scope.todos = response.data;
});

Ext.Ajax.request with date

I am using ExtJs and I need to pass a date in Ext.Ajax.request, but I dont know how can I do it. I am trying the following code:
dateController: function(botao){
Ext.Ajax.request({
url: Webapp.link("research/2012-09-18T14:30:00/8"),
method: 'get',
success: function(response){
console.log(response);
}
});
}
The first one parameter in URL is the date and second one is the product id.
How about
dateController: function(botao){
Ext.Ajax.request({
url: "YourUrl?research=2012-09-18T14:30:00",
method: 'get',
success: function(response){
console.log(response);
}
});
}
Or you could do it using the params (I never tried this with get, but it should work as well)
Ext.Ajax.request({
url : 'YourUrl',
method:'get',
params : {
research: Ext.encode("2012-09-18T14:30:00")
},
scope : this,
//method to call when the request is successful
success : this.onSuccess,
//method to call when the request is a failure
failure : this.onFailure
});

Resources