Rectangle 27 0

javascript HTTP Content Type Header and JSON?


Content-Disposition: attachment; filename=myfile.json
header('Content-Type: application/json');
myfile.json
myfile.json.html

@RemiGrumeau What is 'not totally true'? Downloading files with browser is something completely different. The browser will probably default to expect HTML, so it assumes anything it receives is HTML unless otherwise specified. When downloading, it appends .html to the file, because that's what it defaults to.

I don't know the full context of the problem here -- BUT, browsers (and javascript) do care about Content-Type sometimes. This header can impact the heuristics a browser uses to display content, and sending XML and JSON with a content-type of text/html can often create subtle bugs in the underlying XHR requests (or your framework's layers on top of those)

The Content-Type header is just used as info for your application. The browser doesn't care what it is. The browser just returns you the data from the AJAX call. If you want to parse it as JSON, you need to do that on your own.

The header is there so your app can detect what data was returned and how it should handle it. You need to look at the header, and if it's application/json then parse it as JSON.

This is actually how jQuery works. If you don't tell it what to do with the result, it uses the Content-Type to detect what to do with it.

Note
Rectangle 27 0

javascript HTTP Content Type Header and JSON?


alert(response.text);//will alert "[object Object]" string
console.log(response.text);//will logging all data object
echo "'" . json_encode(array('text' => 'omrele')) . "'";
//alert(response.text) will alert {"text":"omrele"}
echo '<script>var returndata=';
echo '"' . json_encode(array('text' => 'omrele')) . '"';
echo ';</script>';

//it will return wrong javascript code: 
<script>var returndata="{"text":"omrele"}";</script>

Content-Type: application/json is just content header, the content header is just information about type of returned data, ex::JSON,image(png,jpg,etc..),html. Keep in mind, that JSON in javascript is an array or object. if you want to see all the data, use console.log instead of alert

Do not use double quotes, it will confuse javascript, because JSON uses double quote on each value and key:

if you want to alert original JSON as string, than add single quotation marks ('):

Note
Rectangle 27 0

javascript HTTP Content Type Header and JSON?


Content-Disposition: attachment; filename=myfile.json
header('Content-Type: application/json');
myfile.json
myfile.json.html

@RemiGrumeau What is 'not totally true'? Downloading files with browser is something completely different. The browser will probably default to expect HTML, so it assumes anything it receives is HTML unless otherwise specified. When downloading, it appends .html to the file, because that's what it defaults to.

I don't know the full context of the problem here -- BUT, browsers (and javascript) do care about Content-Type sometimes. This header can impact the heuristics a browser uses to display content, and sending XML and JSON with a content-type of text/html can often create subtle bugs in the underlying XHR requests (or your framework's layers on top of those)

The Content-Type header is just used as info for your application. The browser doesn't care what it is. The browser just returns you the data from the AJAX call. If you want to parse it as JSON, you need to do that on your own.

The header is there so your app can detect what data was returned and how it should handle it. You need to look at the header, and if it's application/json then parse it as JSON.

This is actually how jQuery works. If you don't tell it what to do with the result, it uses the Content-Type to detect what to do with it.

Note
Rectangle 27 0

javascript HTTP Content Type Header and JSON?


alert(response.text);//will alert "[object Object]" string
console.log(response.text);//will logging all data object
echo "'" . json_encode(array('text' => 'omrele')) . "'";
//alert(response.text) will alert {"text":"omrele"}
echo '<script>var returndata=';
echo '"' . json_encode(array('text' => 'omrele')) . '"';
echo ';</script>';

//it will return wrong javascript code: 
<script>var returndata="{"text":"omrele"}";</script>

Content-Type: application/json is just content header, the content header is just information about type of returned data, ex::JSON,image(png,jpg,etc..),html. Keep in mind, that JSON in javascript is an array or object. if you want to see all the data, use console.log instead of alert

Do not use double quotes, it will confuse javascript, because JSON uses double quote on each value and key:

if you want to alert original JSON as string, than add single quotation marks ('):

Note