angular-http

AngularJS using an interceptor to handle $http 404s - promise not defined error

…衆ロ難τιáo~ 提交于 2019-12-02 20:38:52
I have an Angular app, for which I want to handle 404s form an API end point. The key components are like so: // app.js var myApp = angular.module('myApp', ['ngRoute',]); myApp.config( function ($httpProvider, $interpolateProvider, $routeProvider) { $httpProvider.interceptors.push('httpRequestInterceptor'); $routeProvider ... .when('/project/:projectId', { templateUrl : 'partials/project_detail.tmpl.html', controller: 'ProjectDetailCtrl', resolve: { project: function ($route, ConcernService) { return ConcernService.get('projects/', $route.current.params.projectId); }, } }); }); // interceptors

cannot retrieve data from angular http

好久不见. 提交于 2019-12-02 18:22:37
问题 I'm trying to retrieve data from a collection in my mongodb using http module using the code below, getPosts() { return this.http.get('http://localhost:5005/blog/getposts').map(res => { console.log("mapped result",res); res.json() }); } It fails everytime with a response Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) at Response.webpackJsonp.../../../http/@angular/http.es5.js.Body.json (http.es5.js:797) at MapSubscriber.project (auth.service.ts:45) at MapSubscriber

AngularJS: Performing $http request inside custom service and returning data

旧城冷巷雨未停 提交于 2019-12-02 17:48:05
I have defined a custom http service in angular that looks like this: angular.module('myApp') .factory('myhttpserv', function ($http) { var url = "http://my.ip.address/" var http = { async: function (webService) { var promise = $http.get(url + webService, { cache: true }).then(function (response) { return response.data; }); return promise; } }; return http; }); And I can access this service in my controller like so: angular.module('myApp') .controller('myCtrl', function (myhttpserv) { var webService = 'getUser?u=3' myhttpserv.async(webService).then(function (data) { console.log(data); }) });

Angular 2 observable-subscribe showing undefined

余生颓废 提交于 2019-12-02 10:14:26
问题 I am having the same challenge as was faces in the SO Post here I am getting undefined in the subscribe method in my component.ts, even though in my service i have the data. See codes below p.component.ts private getPayItems():void{ console.log('In getPayItems'); this._payItemService.getPayItems() .subscribe(data => { this.payItemArray = data; console.log(data); }, (error:any) =>{ this.alerts.push({ msg: error, type: 'danger', closable: true }); }) } p.service.ts getPayItems():Observable

HTTPS with $http in Angular not working

纵饮孤独 提交于 2019-12-02 05:11:24
问题 I'm building a Facebook Tab using Angular. I'm doing a $http request to a PHP page on the same domain. The request looks like this: $http({ method: 'JSONP', url: '/api?action=saveResult&points=' + state.points + '&callback=JSON_CALLBACK', cache: false }); The app is served with HTTPS but when I try to run the app in a Facebook tab I get the following mixed content error: Mixed Content: The page at 'https://example.com' was loaded over HTTPS, but requested an insecure script 'http://example

Angular 2 Http – How to Get JSON Data from API with finance_charts_json_callback() callback

断了今生、忘了曾经 提交于 2019-12-01 22:04:58
问题 I'm trying to get json data from this api: http://chartapi.finance.yahoo.com/instrument/1.0/NFLX/chartdata;type=quote;range=1d/json And I don't know how to get into the returned finance_charts_json_callback(). I'm using Angular 2's http.get(): loadData() { return this.http .get(this.url) .map((res) => res.json()) .subscribe((data) => console.log(data)); } When it gets to => res.json() , it throws this error: EXCEPTION: SyntaxError: Unexpected token i 回答1: You need to use JSONP in this case

Getting response headers from HttpClient post request in Angular?

不想你离开。 提交于 2019-12-01 20:16:45
I'm trying to get the response headers from a post request, but the HttpResponse object doesn't contain the same headers that I can see in the network. What am I doing wrong? I need to access the value of the Apiproxy-Session-Id key and it isn't present in the HttpHeaders. This is my code to execute the post request and log the full response, where http is an HttpClient object. this.http.post('http://localhost:8081/user/login', JSON.stringify(requestBody), {observe: 'response'}).subscribe(resp => { console.log(resp); }); This is the response I'm logging. These are the headers I'm seeing in the

Angular 4 How to return multiple observables in resolver

你说的曾经没有我的故事 提交于 2019-12-01 19:52:20
Basically as a title states, I need to return multiple observables or maybe a results. The goal is basically to load lets say a library list and then load a books based on that library ID's. I don't want to call a service in components, instead I want all the data to be loaded before the page load. import { Injectable } from '@angular/core'; import { Resolve, ActivatedRouteSnapshot } from '@angular/router'; import { UserService } from './../_services/index'; @Injectable() export class LibraryResolver implements Resolve<any> { constructor(private _userService: UserService) {} resolve(route:

Angular 4 How to return multiple observables in resolver

元气小坏坏 提交于 2019-12-01 17:34:22
问题 Basically as a title states, I need to return multiple observables or maybe a results. The goal is basically to load lets say a library list and then load a books based on that library ID's. I don't want to call a service in components, instead I want all the data to be loaded before the page load. import { Injectable } from '@angular/core'; import { Resolve, ActivatedRouteSnapshot } from '@angular/router'; import { UserService } from './../_services/index'; @Injectable() export class

How do I write an Angular 2 service with CORS in Ionic?

ε祈祈猫儿з 提交于 2019-12-01 08:32:11
问题 I'm having trouble moving my Angular 1 JavaScript service to a Angular 2 TypeScript service using http to make a CORS request (this is using Ionic version 2). In Angular 1, I do something like this. angular.module('app.services',[]) .factory('LoginService', ['$http', function($http) { var service = {}; service.isUserLoggedIn = function() { var restUrl = 'http://10.10.10.25:8080/api/user/isloggedin'; var options = { method: 'GET', url: restUrl, withCredentials: true, headers: { 'x-request-with