Json not working from my php server


#1

Continuing the discussion from Array from php server:

I have set header as https://randomuser.me/api/?results=5 but still my http://need4engineer.com/api.php is not working ase json data.

What is the problem ?


#2

What you mean by “not working as json data”?


#3

I went througn this http://blog.ionic.io/5-minutes-with-ionic-2-calling-an-api/ tutorial and
this.http.get(‘https://randomuser.me/api/?results=5’) this is working well. But when I replace the code this.http.get(‘http://need4engineer.com/api.php’) then not working.

though both https://randomuser.me/api/?results=5 & http://need4engineer.com/api.php giving same output with same header.


#4

@arctushar Check for errors in the console.

I created a plunker based on the code in the blog post and I’m getting the following error:

XMLHttpRequest cannot load http://need4engineer.com/api.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ‘http://run.plnkr.co’ is therefore not allowed access.

I guess that you might need to enable CORS.


#5

:slight_smile: thanx so much. Now its working. This will help me lot.


#6

For your kind information, enabling CORS solved this. I mean I have written below code in php. And its work fine

//header(‘Content-Type:application/json; charset=utf-8’);
header(“Access-Control-Allow-Origin: *”);

So, without header(‘Content-Type:application/json; charset=utf-8’); , json works :slight_smile: