tag:blogger.com,1999:blog-865923359735383241.post9016313187177176551..comments2023-10-29T07:27:09.012-06:00Comments on Ccna final exam - java, php, javascript, ios, cshap all in one: correct HTTP header for json fileUnknownnoreply@blogger.comBlogger4125tag:blogger.com,1999:blog-865923359735383241.post-22922372361515832392012-05-29T01:03:48.236-06:002012-05-29T01:03:48.236-06:00I recently had a very strange run in with this.
I...I recently had a very strange run in with this.<br /><br />I had a form which used jQuery to do an AJAX lookup against a PHP script, and then return the response as JSON, formed using PHP's json_encode() function.<br /><br />It was all working fine for a couple of months, then this morning it stopped working... It turns out the javascript couldn't parse the JSON correctly.<br /><br />My original method used eval.<br /><br />I.e.<br /><br />var p = eval('(' + msg + ')');<br /><br /><br />But that gave me the error:<br /><br />missing ] after element list<br /><br /><br />I then tried using <br /><br />var p = JSON.parse(msg);<br /><br /><br />Using the json2.js library from http://json.org. This also failed.<br /><br />I checked my JSON was formatted correctly using an online tool - it was.<br /><br />In the end I tried changing the header type I was setting in the PHP script which was being called by jQuery AJAX.<br /><br />I changed it from application/json to text/plain<br /><br />And both the eval() and the JSON.parse solutions immediately worked... Much to my relief as I was running out of ideas.<br /><br />Very odd and still don't know quite why, but thought I'd post here in case someone runs into a similar issue, or anybody can provide an explanation.Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-1596267380580412232012-05-29T01:03:47.626-06:002012-05-29T01:03:47.626-06:00application/json is favored but usually web browse...application/json is favored but usually web browsers don't know what to do with those headers and screws it up. For stuff like jquery, I have seen text/html recommended. If you are having errors pop up (e.g. download dialog box) then try text/htmlUserhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-10638465828409022382012-05-29T01:03:44.042-06:002012-05-29T01:03:44.042-06:00Look at things like the Apache Axis spec. They fa...Look at things like the Apache Axis spec. They favor application/json.Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.comtag:blogger.com,1999:blog-865923359735383241.post-50566644535755247402012-05-29T01:03:43.453-06:002012-05-29T01:03:43.453-06:00AFIK IANA has officially registered a MIME type fo...AFIK IANA has officially registered a MIME type for JSON as application/json in RFC4627, also is listed in the Internet Media Type list.Userhttps://www.blogger.com/profile/11557173689529910046noreply@blogger.com