I am trying to access an URL in AngularJS as
$http.get("http://localhost:8080/#/notebook/2CG2BVYJ1")
.then(function(response){ vm.getgraph = response.data; });
For this, the get request sent is only http://localhost:8080/. I think $http.get is ignoring anything after '#' in the URL. How to make it access the full URL?
If this is is from the same app I will suggest you can use get in more direct way like so
$http({
method: 'GET',
url: '/notebook/2CG2BVYJ1'
}).then(function(response){ vm.getgraph = response.data; });
I hope this help
Related
I am using fuse template and accessing my web service by using $http, its working fine if i am using method: 'POST' without send any data but whenever i am adding some data with post like: data: {text:'test'} and send request to my web service its change the method type POST to OPTIONS.
My Code:
$scope.submit = function(){
$http({
method: 'POST',
url: 'http://www.example.com/api-link',
data: {test: 'hello'},
header: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).then(function(result){
console.log(result);
});
}
When i am checking in browser network its showing method type OPTIONS. Can anyone please tell what is wrong in my code?
Thanks
I want to pass my $scope.data as data in $http-request.
$scope.data is not empty!
$http({
method: 'PUT',
url: url,
data: $scope.data
})...
But when sending this request the data is empty.
Try this shortcut method
From Angular docs
Look under the Shortcut methods section here
$http.put(url, data, config)
.then(
function(response){
// success callback
},
function(response){
// failure callback
});
If you are using node.js make sure you have included body-parser module and access it by request.body
I have in my javascript the following AngularJS snippet:
$http({
method: 'POST',
url: '/checklogin'
})
.then(function successCallback(response) {
alert('It worked');
}, function errorCallback(response) {
alert('Error:' + response.status)
});
That points in the routes file to a Play Controller:
POST /checklogin controllers.CheckLogin.index
The problem is that for each different Angular HTTP post I need to create an entry in routes (around 200 entries to create and maintain). Is there a way to avoid that?
You can use dynamic matching in routes and match your route somewhere in your application, but it isn't elegant solution.
GET /*yourRoute AppController.matcher(yourRoute)
What is type of data angular sending? I use laravel + angular. I`m trying, but this script return 405 error. Method not allowed.
.controller('adminCtrl', function( $scope, $http ){
$scope.collection = [];
$scope.newData = [];
$scope.newrecord = function() {
$scope.collection.push($scope.newData);
$http({
url: '/newrecord',
method: "POST",
data: $.param($scope.collection),
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
}).success(function(data){
console.log(data);
})
}
})
You are getting 405 - Method not Allowed because the server you are sending your request does not have POST it the white list of methods allowed to be used to perform requests to that given API.
It's not an angularJS issue, it's a server configuration issue.
$http sends data as json.
You do not need to serialize params using "$.param", data is plain javascript object, which is send to your REST endpoint.
So attach just "$scope.collection) and do not set Content Type manually, it is json by default.
POST can be send also with convenience method.
$http.post('/someUrl', data, config).then(successCallback, errorCallback);
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