FAQ Database Discussion Community


AngularJS: $http service GET to php server: Request Method:OPTIONS, Status Code: 405 Method Not Allowed

angularjs,get,angular-http
I'm struggling with this issue since many hours... :-( My grunt-served angular app issues an $http GET request to a simple PHP service on the same host (apache), to get a list of persons. The result I get is an OPTIONS request (???), which gets a 405 response... But why...

AngularJS - Cannot read response headers from $http

angularjs,http-headers,angular-http
My http response contains the headers Authentication (as mentioned here: Authentication:76efbc0946773b62c93e952b502a47acd898200f6f80dc46ac87ffc501c00780) when I inspect the request with the inspector, but a call to headers("Authentication") returns null return $http({ method: "GET", url: url, headers: { 'Content-Type': "application/json" } }).success(function (data, status, headers, config) { console.log(headers("Authentication")); }) Do you have any idea...

Unit Test Angular http.post with Data in Header

angularjs,unit-testing,jasmine,angular-http
I am trying to write a unit test for service that performs a http.post to an api that passes credentials in the header. Controller: app.controller('LoginController', function($scope, $http, signInService) { $scope.LogIn = function(usrnm, pwd) { signInService.authUser(usrnm, pwd) .success(function (data, status, headers, config) { // Display success message $scope.gotToAddress = data.successUrl; })...

Undefined variable $http request

javascript,angularjs,undefined,angular-http
I'm performing a GET - request on my back end to retrieve some JSON - data. I want to store a part of the data in a variable because I want to use it after the request. But everytime, the variable is undefined while I did the same thing in...

Getting 404 error on API PUT call with route param

angularjs,node.js,express,mean-stack,angular-http
This is the API url I'm trying to hit in my Express app: // Dashboard API to update account app.post('/api/accounts/:id', accountsController.update); My full accounts module with Accounts factory, then specific descriptions below that: (function() { var app = angular.module('app-accounts', ['ngAnimate', 'ngResource', 'account-directives']) .controller('AcctCtrl', ['$scope', '$resource', 'Accounts', function($scope, $resource, Accounts) {...

How to finish executing $http.get first before running any code in angular

angularjs,angular-http
I have this method in my service to get a user getUser : function () { console.log("get user"); token = authService.getToken(); userID = jwtHelper.decodeToken(token); console.log(token); console.log(userID); return $http.get(httpHost + '/users/' + userID).success(function (data) { user = data; console.log(user); return data; }); }, I ran this service in .run of angular...

Using GET only reqeusts on API - $http or $resource

angularjs,angular-resource,angular-http
If all I am doing is making basic GET requests from an API (not full CRUD), is it better to use $http or $resouce? I see that $resouce uses $http under the hood so does that make it overkill? Code is written more streamlined using $resouce than $http espcially when...

Angular - How to properly handle an HTTP error from server?

angularjs,angular-http
Currently I've got this: $http({ method: 'POST', url: 'http://api-endpoint/somescript/', data: formData, headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }) .then(function (response) { console.log(response); }); If the script on the other end works out ok, then gets called. However, let's say the script on the server end has some sort of error that's...

Angularjs interceptor not working

angularjs,angular-http,angular-http-interceptors
I am trying to create a $http interceptor that looks like this: .config(['$httpProvider',function($httpProvider){ $httpProvider.interceptors.push(function($q,$window){ return { 'request':function(config){ config.headers = config.headers || {}; if($window.sessionStorage) config.headers.Authorization = 'Bearer '+$window.sessionStorage.token; }, 'requestError':function(rejection) { $q.reject(rejection); }, 'response':function(response){ return response || $q.when(response); }, 'responseError':function(response) { if(response!=null...

Angular updating $scope variable after ajax is not reflecting in UI

javascript,ajax,angularjs,scope,angular-http
I cannot seem to figure out the issue here. On an ajax success response I set a value in the current controller that is not reflecting in the UI. The general answer I have found is to run with the Angular ajax functions and/or apply $apply or $digest to the...

Create ordered list using data returned from API

javascript,angularjs,angular-http
I followed a lot of tutorials about this but can't have this working. I want to create an ordered list with a manipulated data received via API. I create a factory for each WebService I need to ask. Factory: angular.module('aparcare').factory('Items', function ($http) { return { get: function (fields) { return...

How to read response headers in angularjs?

angularjs,angular-http
My server returns this kind of header: Content-Range:0-10/0: I tried to read this header in angular with no luck: var promise = $http.get(url, { params: query }).then(function(response) { console.log(response.headers()); return response.data; }); which just prints Object {content-type: "application/json; charset=utf-8"} Any ideas how to access the content range header?...

how does Angular $http cache key get created

angularjs,angular-http
It does not specific in the Angular docs how $http checks the cache for existing keys. For example, when I do: $http.get("/search/11", { cache: true }); The cache will obviously work. But what if i do $http.get("/search", { cache: true, params: { age: 11 } }); Or the more complicated...

Angularjs $http delete with $q promise leads to TypeError: object is not a function

javascript,angularjs,rest,angular-promise,angular-http
The following snippet returns the following error: TypeError: object is not a function service.deleteItem = function(itemId) { var def = $q.defer(); $http.delete(SERVER_REST_PATH + '/items/' + itemId) .success(function() { def.resolve(); }).error(function(data, status) { def.reject("Error deleting the item"); }); return def.promise(); }; If I rewrite it as the following it works: service.deleteItem...

Use $http once from Angular Service

angularjs,angular-http
I need to grab some data from my db through an API and make it accessible throughout my Angular app. I understand that Services are good for storing data to be accessed from multiple controllers. However, in the following code I end up with a new $hhtp.get() each time just...

How to prioritize requests in angular $http service?

angularjs,angular-http,angular-http-interceptors
I'm working on an application with a large amount of lazy data loading. I would like to prioritize http requests based on 'priority' param. This is the concept of using it. $http.get(url, {params: query, priority: 1}) I was thinking of using $http interceptors. Something like that: angular.module('myModule') .factory('httpPriorityInterceptor', function ($interval,...

param 'data' is undefined in transform request of $httpProvider

javascript,ajax,angularjs,angular-http
I'm tryng to make a request transform using the $httpProvider as this: angular.module('myApp') .config(function ($httpProvider, requestNotificationProvider) { $httpProvider .defaults .transformRequest .push(function (data) { requestNotificationProvider .fireRequestStarted(data); return data; }); }); The requestNotificationProvider is a provider that is supposed to perform some actions, but the point is that on debug, in every...

Getting data from a web service with Angular.js

angularjs,angularjs-scope,angular-http
Im trying to get data in a Json format from a remote WS using Angular and im having some trouble. The data comes from the web service correctly but i cant use it inside the controller. Why is that? Angular Code: var booksJson; var app = angular.module('booksInventoryApp',[]); // get data...

Populate an array with data from database (Angular + PHP/MySQL)

angularjs,angular-http
I have a simple to-do app I'm working on, which is using Angular and then PHP/MySQL for the backend. I now have a simple app that works, where I can add new todos, and add the "percentage completed" per day to the Database, using a simple $http post. However now...

How to send a valid crsf token with angularjs $post service and symfony2

ajax,symfony-forms,angularjs-service,csrf-protection,angular-http
I think I read enough topics for posting this question... and I really want to know why my submission using ajax is invalid (The CSRF token is invalid. Please try to resubmit the form). This is how I send my form data and the csrf token. <form name="ReviewForm" ng-init="formData =...

Angularjs - Abort/cancel running $http calls

angularjs,autocomplete,angular-resource,angular-http
I've got a call using Resource in angularjs but i get some problems because i can't abort every calls it does. This kind of structure i use for an autocomplete.. is it possible convert from resource call to http? This is the code var Resource = $resource(URL, {},{ getAutocompleteResults: {...

Is it possible to check condition before all http calls in Angular?

angularjs,angular-http
Is it possible to check if: $rootScope.variable is TRUE Before all $http calls are called, or I should check for every individual call? Some of my calls are call through angular factory, and some are not. I thought there is maybe some way like httpInterceptor which would check before some...

Can't send a POST request with $http in Angular - ReferenceError: $http is not defined

angularjs,angular-http
So I keep getting this ReferenceError: $http is not defined, even though I have included $http in the controller, which seems to be the most common cause of this error message. I've tried also passing $http into the function itself, but that doesn't solve it. I feel like I am...

Angular $http params encoding issue

javascript,angularjs,angular-http
I have an Angular 1.4 service that makes an $http.get() request to the MapQuest API. If I pass my API key as part of the url directly then it works however if I add it as part of the params field then it does not. Working: var url = 'http://open.mapquestapi.com/geocoding/v1/address?key=Gmjtd%7Cluub2d08nh%2C2s%3Do5-2u2gh4';...

How to read JSON error response from $http if responseType is arraybuffer

javascript,angularjs,angular-http
I load some binary data using $http.post(url, data, { responseType: "arraybuffer" }).success( function (data) { /* */ }); In case of an error, the server responds with an error JSON object like { "message" : "something went wrong!" } Is there any way to get the error response in a...