angular-http

How does HTTP error-handling work with observables?

流过昼夜 提交于 2019-12-20 12:08:17
问题 I see a lot of tutorials doing something like this: http.get("...").subscribe( success => console.log('hello success'), error => console.log('bye error') ); I don't know how this works, since there aren't any types or anything, however I tried to do that myself and I end up that the request always goes into success, even if I have an error. What is the problem? Troublemaker: this.memberService.create(this.currentMember) .subscribe( success => { let mem: Member = success.json() as Member; if

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

戏子无情 提交于 2019-12-20 09:37:20
问题 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)

Base 64 Image to ocr.space API Ionic 2

北慕城南 提交于 2019-12-20 04:40:31
问题 I am trying to send a base 64 Jpeg to an API for OCR analysis. The API docs can be found here https://ocr.space/ocrapi The code to save the Image is here: takePicture() { Camera.getPicture({ destinationType: Camera.DestinationType.DATA_URL, targetWidth: 1000, targetHeight: 1000, encodingType: Camera.EncodingType.JPEG, sourceType: Camera.PictureSourceType.CAMERA, allowEdit:true }).then((imageData)=>{ this.base64Image = "data:image/jpeg;base64," + imageData; }); } However I'm sure this is all

How to prioritize requests in angular $http service?

若如初见. 提交于 2019-12-18 13:39:45
问题 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, $q) { var requestStack = []; return { request: function (config) { config.priority = config.priority || 3; requestStack.push

Angular HttpClient params with method GET do not work

和自甴很熟 提交于 2019-12-18 09:06:43
问题 I wanna ask you about params with get method. I have something like this: path = 'https://example.com/api'; const params = new HttpParams(); params.append('http', 'angular'); return this.http.get(path, {params: params}); I thought I would have url like: www.example.com/api?http=angular but actually when I check that in network tab in chrome, request url is www.example.com/api What should I do when I want to have path: www.example.com/api?http=angular And is it possible to check request url of

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

我们两清 提交于 2019-12-17 22:56:15
问题 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 different type than a success response? $http.post(url, data, { responseType: "arraybuffer" }) .success(function (data) { /* */ }) .error(function (data) { /* how to access data.message ??? */ }) 回答1: Edit: As @Paul

No methods in http response object

依然范特西╮ 提交于 2019-12-17 06:16:08
问题 Object returned by $http.get has no methods. Example: I have my class model export class Lab { constructor( public id: number, public name: string, public description: string, public isActive: boolean, public classes: Classes[] ) { } isActive(lab: Lab) { return this.isActive; } } in my service I call http fetching lab getLab(labId: number) { return this.http.get<Lab>(DidacticsServiceUrls.apiRoot + labId).toPromise(); } and when I get this in some component, method isActive is undefined, so

How can I post data as form data instead of a request payload?

三世轮回 提交于 2019-12-16 19:03:53
问题 In the code below, the AngularJS $http method calls the URL, and submits the xsrf object as a "Request Payload" (as described in the Chrome debugger network tab). The jQuery $.ajax method does the same call, but submits xsrf as "Form Data". How can I make AngularJS submit xsrf as form data instead of a request payload? var url = 'http://somewhere.com/'; var xsrf = {fkey: 'xsrf key'}; $http({ method: 'POST', url: url, data: xsrf }).success(function () {}); $.ajax({ type: 'POST', url: url, data

How to add HttpClient Interceptors conditionally in Angular

做~自己de王妃 提交于 2019-12-14 04:16:10
问题 Recently I have been using Interceptors with Angular HttpClient. I add headers corresponding to some HTTP GET methods and for some I do not need those headers. How can I tell my interceptor to conditionally add interceptors to only those methods? I can even split up services like one service for headers and one without headers or one for different headers and one for different. NgModule providers { provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true, },{ provide: HTTP

AngularJS global $http state ajax loader

这一生的挚爱 提交于 2019-12-14 03:42:52
问题 I have an AngularJS app, and need an ajax loader for every request done by the $http - is there a simple way to do this. My solution now is to set $rootScope.loading = 1 everytime i call a $http, and on the success set $rootScope.loading = 0.. What is the "best practice" for this? My code now looks like: $rootScope.loading = 1; $http({method : "POST", url:url, data: utils.params(data), headers: {'Content-Type': 'application/x-www-form-urlencoded'}}).success(function() { $rootScope.loading = 0