Why we use
header('Content-Type: application/json');
Before
echo json_encode($data)
in the web services in php
If I will not use header('Content-Type: application/json');
what is its disadvantage?
Help will be appreciated
Why we use
header('Content-Type: application/json');
Before
echo json_encode($data)
in the web services in php
If I will not use header('Content-Type: application/json');
what is its disadvantage?
Help will be appreciated
You should always set Content-Type: application/json if you are sending json. Headers are used to tell the other side what kind of data you are sending and is a best practice.
When you are defining the Content-Type in headers of the any response, tell the client about which type of content they are receiving in response. When we are sending the JSON encode string in the response we should always set Content-Type:application/json in headers.
Default "Content-Type:text/html" is set in response PHP headers.