What is the correct JSON content type ?

01 Aug, 2021
Content-Type is an HTTP header that is used to indicate the media type of the resource and in the case of responses, it tells the browser about what actually content type of the returned content is. In case of any POST or PUT requests, the client tells the server about the kind of data sent.To know about the type of content the browser is going to encounter, it does a MIME sniffing. MIME or Multipurpose Internet Mail Extension is a specification for non-text e-mail attachments. It allows the mail client or Web browser to send and receive different file formats as an attachment over the Email. For receiving a JSON request, it is important to mention or tell the browser about the type of request it is going to receive. So we set its MIME type by mentioning it in the Content-Type. We can do the same in two ways:  
MIME type: application/json
MIME type: application/javascript
MIME type: application/json It is used when it is not known how this data will be used. When the information is to be just extracted from the server in JSON format, it may be through a link or from any file, in that case, it is used. In this, the client-side only gets the data in JSON format that can be used as a link to data and can be formatted in real-time by any front end framework.  
Example: In this example, the MIME-type is application/json as it is just extracting the dictionary from that variable and putting it in JSON format, and showing it.  
php
<?php// Setting the headerheader('Content-Type:application/json'); // Initializing the directory$dir =[    ['Id'=> 1, 'Name' => 'Geeks' ],    ['Id'=> 2, 'Name' => 'for'],    ['Id'=> 3, 'Name' => 'Geeks'],      ];// Shows the json dataecho json_encode($dir);?>
Output:  
[{"Id":1, "Name":"Geeks"}, {"Id":2, "Name":"for"}, {"Id":3, "Name":"Geeks"}]
MIME type: application/javascript It is used when the use of the data is predefined. It is used by applications in which there are calls by the client-side ajax applications. It is used when the data is of type JSON-P or JSONP. JSONP or JavaScript Object Notation with Padding is used when the API is wrapped in a function call. The function is defined in the client-side JavaScript code and the API is passed to it as a parameter and thus it acts as executable JavaScript code. 
Example: In this example, the MIME-type is application/javascript as it is just extracting the dictionary from a variable, extract it in JSON format and then send it as a parameter to a function call at a client-side.  
php
<?php // Using application/javascriptheader('Content-Type:application/javascript');$dir =[    ['Id'=> 1, 'Name' => 'Geeks' ],    ['Id'=> 2, 'Name' => 'for'],    ['Id'=> 3, 'Name' => 'Geeks'],      ]; // Making a function call to the client side // using Function_call()// Sending JSON data as a parameter to client.echo "Function_call(".json_encode($dir).");"; ?>
Output:  
Function_call([{"Id":1, "Name":"Geeks"}, {"Id":2, "Name":"for"}, 
{"Id":3, "Name":"Geeks"}])
It is recommended to use application/json instead of application/javascript because the JSON data is not considered as a javascript code. It is a standard and thus is given a separate content type as i.e. application/json.  
krshubhamam025
JSON
PHP-Misc
Picked
PHP
Web Technologies
Web technologies Questions
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to execute PHP code using command line ?
PHP in_array() Function
How to delete an array element based on key in PHP?
How to Insert Form Data into Database using PHP ?
How to convert array to string in PHP ?
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?