angular-http

$http.post() method is actally sending a GET

拥有回忆 提交于 2019-11-29 13:21:15
NOTE: I've found a possibly related issue that warrants a new question here This is a weird problem. I've been using angular over the course of 2 years and have never run into this problem. I'm using angular v1.5.0. I'm making a post request like this: $http({ method: "POST", url: "/myurl", data: { file: myFile // This is just an object } }); Run-of-the-mill POST request right? Get this. I look in the console and the Network tab logs the request as a GET. Bizarre. So I've jiggered the code to work like this: $http.post("/myurl", {file: myFile}); Same thing. After stepping through the $http

angularjs $http.get to get json not working in the service layer

纵饮孤独 提交于 2019-11-29 12:20:02
问题 I am developing an angularjs app as a part of my angularjs learning. I have controllers and from there I am calling service layers. leagueManager.service("teamsService", function($http){ var teams = {}; $http.get('data/teams.json').then(function(data) { teams = data; }); this.getTeams = function(){ return teams; }; }); I noticed that because of the asynchronous nature of $http.get.then stuff, the data is not retrieved immediately and hence I would not get the "teams" when I would call

Angular 2 http request with Access-Control-Allow-Origin set to *

痴心易碎 提交于 2019-11-29 05:41:29
I'm using angular2 and typescript. I'm trying to post to my mail chimp subscription list. My code so far: constructor(router: Router, http: Http){ this.router = router; this.http = http; this.headers = new Headers(); this.headers.append('Content-Type', 'application/json'); this.headers.append('Access-Control-Allow-Origin', '*'); } subscribe = () => { var url = "https://thepoolcover.us10.list-manage.com/subscribe/post?u=b0c935d6f51c1f7aaf1edd8ff&id=9d740459d3&subscribe=Subscribe&EMAIL=" + this.email; this.isSuccess = false; this.http.request(url, this.headers).subscribe(response => { console

Getting data from a web service with Angular.js

别来无恙 提交于 2019-11-29 01:17:42
问题 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 from the WS app.run(function ($http) { $http.get("https://SOME_API_PATH").success(function (data) { booksJson = data; console.log(data); //Working }); }); app.controller('booksCtrl', function ($scope) {

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

廉价感情. 提交于 2019-11-28 21:03:36
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 ??? */ }) Edit: As @Paul LeBeau points out, my answer assumes that the response is ASCII encoded. Basically you just need to decode

Error handling in AngularJS http get then construct

北城以北 提交于 2019-11-28 18:31:16
How can I handle an HTTP error, e.g. 500, when using the AngularJS "http get then" construct (promises)? $http.get(url).then( function(response) { console.log('get',response) } ) Problem is, for any non 200 HTTP response, the inner function is not called. You need to add an additional parameter: $http.get(url).then( function(response) { console.log('get',response) }, function(data) { // Handle error here }) You can make this bit more cleaner by using: $http.get(url) .then(function (response) { console.log('get',response) }) .catch(function (data) { // Handle error here }); Similar to @this.lau

Angular HTTP: Status -1 and CORS

半世苍凉 提交于 2019-11-28 13:47:45
In our angular application sometimes we get http status -1 returned to us. The status -1 happens on a popup that is closed, so the user isn't affected by it, just our logs. I attempted to handle it by doing switch (response.status) { case 0: break; case -1: break; case 401: localStorageService.clearAll(); redirectToUrlAfterLogin.url = $location.path(); $location.path('/login'); Which was suggested in AngularJS Issue #12920 We are definitely getting less logs in, but there are still some HTTP -1 status codes. Is there a different way I should be handling the -1? Muthukannan Kanniappan When the

Accessing API with $http POST Content-Type application/x-www-form-urlencoded

二次信任 提交于 2019-11-28 13:05:09
I am trying to access this REST API, which accepts three parameters: stationId , crusherId , monthYear I am doing it like this in AngularJS as: $http({ //headers: {'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'}, //headers: {'Content-Type': 'application/json; charset=UTF-8'}, headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 'Accept': 'application/json' }, url: 'https://myurl../api/getHPData', method: 'POST', data: { stationId: 263, crusherId: 27, monthYear: '2016-4' } }) .then(function(data, status, headers, config) { //console.log(JSON

Getting JSON for Angular 2 from HTTP remote server fails, but succeeds localy

随声附和 提交于 2019-11-28 10:51:37
问题 I'm trying to read a JSON file using htttp.get in Angular2. When I use a file stored localy on my project, it works OK . But - when I use a remote server , with exactly the same file, I get the following error: 404 - Not Found Collection 'undefined' not found Browsing to http://example.com/my.json , I can see the file on any browser. Here is my typescript code for getting the file: private url = '../my.json'; <--- This works private url = 'http://example.com/my.json'; <--- This throws the

Accessing custom http response headers in angularjs

一曲冷凌霜 提交于 2019-11-28 10:05:12
I am trying to access the header 'error-detail' as you can see in the browser network inspector (link above), the header gets returned. Server-wise I have also added the custom header to the 'Access-Control-Expose-Headers' to allow cross-domain requests as this was suggested to be the fix on other questions. Below is the request to the server along with the success/error callbacks. this.signon = function (request, onComplete, onError) { console.log("Calling server with 'login' request..."); return $http.post("http://localhost:8080/markit-war/services/rest/UserService/login/", request) .then