Ionic: How to work with native HTTP?

I am unable to get the data from the server using Native HTTP. The response on the server looks like:

[{feed_id:“1”,feed_title:“Helping the poor and needy people is a good deed”,feed_date:“2017-07-14”,feed_content:“Poverty is a signal of the failure to acquire suitable resources for the achievement of one’s ends.”,feed_likes:“11”,feed_comments:“12”,feed_has_image:“0”,feed_img_url:“no image”},{feed_id:“2”,feed_title:"Information technology in India ",feed_date:“2017-07-12”,feed_content:“Indias IT Services industry was born in Mumbai in 1967 with the establishment of the Tata Group in partnership with Burroughs. The first software export zone, SEEPZ – the precursor to the modern-day IT park – was established in Mumbai in 1973.”,feed_likes:“20”,feed_comments:“15”,feed_has_image:“0”,feed_img_url:“no image”}]

and my code is as follows:

this.http.get('my.php.url', {}, {})
			.then(data => {

				console.log(data.status);
				console.log(data.data); // data received by server
				console.log(data.headers);

				this.feeds = data.data;

			})
			.catch(error => {

				console.log(error.status);
				console.log(error.error); // error message as string
				console.log(error.headers);

			});

It is very difficult to check the error on device everytime I make change in the code.

below is the log I got from chrome://inspect/#devices

200 main.js:46602
[{feed_id:“1”,feed_title:“Helping the poor and needy people is a good deed”,feed_date:“2017-07-14”,feed_content:“Poverty is a signal of the failure to acquire suitable resources for the achievement of one’s ends.”,feed_likes:“11”,feed_comments:“12”,feed_has_image:“0”,feed_img_url:“no image”},{feed_id:“2”,feed_title:"Information technology in India ",feed_date:“2017-07-12”,feed_content:“Indias IT Services industry was born in Mumbai in 1967 with the establishment of the Tata Group in partnership with Burroughs. The first software export zone, SEEPZ – the precursor to the modern-day IT park – was established in Mumbai in 1973.”,feed_likes:“20”,feed_comments:“15”,feed_has_image:“0”,feed_img_url:“no image”}] main.js:46603
Object {X-Xss-Protection: “1; mode=block”, X-Android-Selected-Transport: “http/1.1”, Access-Control-Allow-Origin: “*”, Date: “Sat, 15 Jul 2017 06:19:52 GMT”, Content-Length: “719”…}
main.js:46604
ERROR Error {} main.js:1362
defaultErrorLogger main.js:1362
ErrorHandler.handleError main.js:1422
IonicErrorHandler.handleError main.js:113941
ApplicationRef_.tick main.js:5360
(anonymous function) main.js:5233
t.invoke polyfills.js:3
inner.inner.fork.onInvoke main.js:4427
t.invoke polyfills.js:3
r.run polyfills.js:3
NgZone.run main.js:4295
ApplicationRef_._this._zone.onMicrotaskEmpty.subscribe.next main.js:5233
schedulerFn main.js:4129
SafeSubscriber.__tryOrUnsub main.js:36538
SafeSubscriber.next main.js:36485
Subscriber._next main.js:36425
Subscriber.next main.js:36389
Subject.next main.js:42443
EventEmitter.emit main.js:4115
NgZone.checkStable main.js:4392
NgZone.setHasMicrotask main.js:4476
inner.inner.fork.onHasTask main.js:4439
t.hasTask polyfills.js:3
t._updateTaskCount polyfills.js:3
r._updateTaskCount polyfills.js:3
r.runTask polyfills.js:3
o

1 Like

Are you sure the response looks like that? That is JS, not JSON. (Validate with jsonlint.com to see what I mean.)

You should add a bit more info to your console.log: console.log('data.status', data.status) so we know what you are outputting here.

Include your ionic info output please.
Post a screenshot of the error message please.

I uesd angular http directly instead of ionic native http. Solved!
Thanks for the reply though. :slight_smile:
BTW any thought on → THIS POST

I can’t remember where I read or figured it out, I had a similar problem though - the Native HTTP doesnt seem to want to cope with a response that’s not an object - yours is an array. If the backend returned instead:

{"feeds":[ ... ]}

It will work, that’s how I fixed mine.

1 Like

Hi, where you able to use POST and sending data to server? We are running into this error:

Is it possible at all to be able to use ionic native HTTP and be able to run in the browser? I would use angular Http but I need the background threading the native HTTP has

Only for personal interest. Why do you need background threadinf? I’m not a security expert :slight_smile:

I want the app to be able to make HTTP requests even if the screen turns off or if its in the middle of the request I dont want it to stop because the screen turned off

For example, my app updates the users location in real time but if the screen turns off then it will stop doing the HTTP POST request and the users location does not get updated. does that make sense?

does it work in ios too ?

What “it” are you asking about?

this is not working in background, there are problems on android and ios working on background. Have a look on cordova background services and something like that