I am working on a project that is done in ionic 3, and I am passing it to ionic 4, but it is giving me problems with http.get calls, it does not return what I expected.
In ionic 3 it worked perfectly.
I am working on a project that is done in ionic 3, and I am passing it to ionic 4, but it is giving me problems with http.get calls, it does not return what I expected.
In ionic 3 it worked perfectly.
Can you share your code? You can check in google dev tools to see if you get date from the get request (F12)
in app.module:
import { HttpClientModule } from ‘@angular/common/http’;
imports: [BrowserModule ,HttpModule,HttpClientModule,IonicModule.forRoot(),ChartsModule, AppRoutingModule],
in service:
import { HttpClient } from ‘@angular/common/http’;
contructor(public http: HttpClient,)
login(url){
this.http.get ( url )
.subscribe (data => {
this.data = data;
}, err=> {
);
}
in the data returns 200, ok but in the body is empty
Sorry if I use the http module returns what I told you, but if I use the HttpClient it returns null
import { Http } from ‘@angular/http’; return 200, ok but body empty
import { HttpClient } from ‘@angular/common/http’; return null
can you add this line:
Console.log(data) check F12 and see if you get data
using http:
Response {_body: “”, status: 200, ok: true, statusText: “OK”, headers: Headers, …}
using HttpClient : null
Post your package.json
file and ionic info
{
“name”: “wapiV2”,
“version”: “0.0.1”,
“author”: “Ionic Framework”,
“homepage”: “https://ionicframework.com/”,
“scripts”: {
“ng”: “ng”,
“start”: “ng serve”,
“build”: “ng build”,
“test”: “ng test”,
“lint”: “ng lint”,
“e2e”: “ng e2e”
},
“private”: true,
“dependencies”: {
“@angular/common”: “^7.2.2”,
“@angular/core”: “^7.2.2”,
“@angular/forms”: “^7.2.2”,
“@angular/http”: “^7.2.2”,
“@angular/platform-browser”: “^7.2.2”,
“@angular/platform-browser-dynamic”: “^7.2.2”,
“@angular/router”: “^7.2.2”,
“@ionic-native/core”: “^5.0.0”,
“@ionic-native/http”: “^5.0.0”,
“@ionic-native/splash-screen”: “^5.0.0”,
“@ionic-native/status-bar”: “^5.0.0”,
“@ionic/angular”: “^4.0.0”,
“chart.js”: “^2.7.3”,
“core-js”: “^2.5.4”,
“ng2-charts”: “^1.6.0”,
“rxjs”: “~6.3.3”,
“rxjs-compat”: “^6.4.0”,
“zone.js”: “~0.8.29”
},
“devDependencies”: {
“@angular-devkit/architect”: “~0.12.3”,
“@angular-devkit/build-angular”: “~0.12.3”,
“@angular-devkit/core”: “~7.2.3”,
“@angular-devkit/schematics”: “~7.2.3”,
“@angular/cli”: “~7.2.3”,
“@angular/compiler”: “~7.2.2”,
“@angular/compiler-cli”: “~7.2.2”,
“@angular/language-service”: “~7.2.2”,
“@ionic/angular-toolkit”: “~1.4.0”,
“@types/node”: “~10.12.0”,
“@types/jasmine”: “~2.8.8”,
“@types/jasminewd2”: “~2.0.3”,
“codelyzer”: “~4.5.0”,
“jasmine-core”: “~2.99.1”,
“jasmine-spec-reporter”: “~4.2.1”,
“karma”: “~3.1.4”,
“karma-chrome-launcher”: “~2.2.0”,
“karma-coverage-istanbul-reporter”: “~2.0.1”,
“karma-jasmine”: “~1.1.2”,
“karma-jasmine-html-reporter”: “^0.2.2”,
“protractor”: “~5.4.0”,
“ts-node”: “~8.0.0”,
“tslint”: “~5.12.0”,
“typescript”: “~3.1.6”
},
“description”: “An Ionic project”
}
Ionic:
ionic (Ionic CLI) : 4.10.2 (C:\Users\usuario\AppData\Roaming\npm\node_modules\ionic)
Ionic Framework : @ionic/angular 4.0.1
@angular-devkit/build-angular : 0.12.4
@angular-devkit/schematics : 7.2.4
@angular/cli : 7.2.4
@ionic/angular-toolkit : 1.4.0
System:
NodeJS : v8.12.0 (C:\Program Files\nodejs\node.exe)
npm : 6.4.1
OS : Windows 10
Use the following:
app.module:
import { HttpClientModule } from ‘@angular/common/http’;
import { HttpModule } from ‘@angular/http’;
...
imports: [
HttpClientModule,
HttpModule,
]
page:
import { HttpClient } from '@angular/common/http';
import 'rxjs/add/operator/map';
...
return this.http.get(<some.url>);
It’s like I’m using it now, but it does not work
show your headers…
Im using like this, I created a function to headers
getHeader(){
let headers = {
headers: new HttpHeaders({
‘Content-Type’: ‘application/json’,
‘Accept’: ‘text/javascript’
})
};
return headers;
}
and use so…
this.http.get(uri, this.getHeader());
Are you sure that there is really a json content in the body? Check in the network tab with F12 in chrome. Because when you used http it gave you body: “”. And httpClient already gives you a deserialized object (and not a response object) which is null if there was no body.
Maybe post the full content of your response including headers here.