javascript - Content-Type Ajax json missing -


i wrote php code outputs valid json, , sets content-type header application/json in dev setup. when deploy script embedded webserver works fine except it's not capable of sending content-type. it's not possible run other webserver.

now have following code dynatable. though dev , embedded webserver, serve same file, , difference content-type. works dev setup, doesn't work embedded setup.

i use following code load json file dynatable.

document.ready(     $.ajax({         url: 'phpapi.php',         success: function(data){             $('#mytable').dynatable({                 dataset: {                     records: data                 }             });         }     })); 

so can explain me why content-type important ajax? how can tell code manually json?

without content-type returned data assumed plain text. there nothing in code tell otherwise.

one way json specify return type in jquery code. add datatype: 'json' ajax configuration.

or use eval() transform returned text json.

document.ready(     $.ajax({         url: 'phpapi.php',         success: function(data){             $('#mytable').dynatable({                 dataset: {                     records: eval(data)                 }             });         }     })); 

using json.stringify(eval(data)) might give better results making sure json.

as pointed out below, json.parse(data) safer. (eval evil after all.)


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -

Why does a .NET 4.0 program produce a system.unauthorizedAccess error on a Windows Server 2012 machine with .NET 4.5 installed? -