hackalive Posted May 5, 2013 Share Posted May 5, 2013 (edited) Hi Guys, I have an ajax script similar to this: $.ajax({ type: "POST", url: "/z/l.php", data: String, dataType: 'json', cache: false, success: function(result) { if(result == ''){ } else { $("#name").val(result.name); $("#add").val(result.add); } } }) However add is an jsopn array like: [{ "Manufacturer": "Toyota", "Sold": 1200, "Month": "2012-11" }, { "Manufacturer": "Ford", "Sold": 1100, "Month": "2012-11" }, { "Manufacturer": "BMW", "Sold": 900, "Month": "2012-11" }, { "Manufacturer": "Benz", "Sold": 600, "Month": "2012-11" }, { "Manufacturer": "GMC", "Sold": 500, "Month": "2012-11" }, { "Manufacturer": "HUMMER", "Sold": 120, "Month": "2012-11" }] However I can not get $.getJSON to work as it is not coming from a URL as result.add is already a JSON string. (the $.ajax is called based on meething certain conditions during keyUp, everything else is working except the JSON string for add - I want to make it a form drop down <select>). Any advice? Cheers in adavnce. Edited May 5, 2013 by hackalive Quote Link to comment https://forums.phpfreaks.com/topic/277637-getjson/ Share on other sites More sharing options...
requinix Posted May 5, 2013 Share Posted May 5, 2013 That was confusing. You're saying result.add is that array? Or is a string containing the JSON for that array? Quote Link to comment https://forums.phpfreaks.com/topic/277637-getjson/#findComment-1428290 Share on other sites More sharing options...
hackalive Posted May 5, 2013 Author Share Posted May 5, 2013 (edited) If I changed "$("#add").val(result.add);" to alert(result.add); it would alert: [{ "Manufacturer": "Toyota", "Sold": 1200, "Month": "2012-11" }, { "Manufacturer": "Ford", "Sold": 1100, "Month": "2012-11" }, { "Manufacturer": "BMW", "Sold": 900, "Month": "2012-11" }, { "Manufacturer": "Benz", "Sold": 600, "Month": "2012-11" }, { "Manufacturer": "GMC", "Sold": 500, "Month": "2012-11" }, { "Manufacturer": "HUMMER", "Sold": 120, "Month": "2012-11" }] (minus the nice formatting of course) Hope that makes sense? If not please let me know. Edited May 5, 2013 by hackalive Quote Link to comment https://forums.phpfreaks.com/topic/277637-getjson/#findComment-1428292 Share on other sites More sharing options...
hackalive Posted May 5, 2013 Author Share Posted May 5, 2013 Update: The JSON sting is valid according to http://jsonlint.com/ Quote Link to comment https://forums.phpfreaks.com/topic/277637-getjson/#findComment-1428298 Share on other sites More sharing options...
Solution hackalive Posted May 5, 2013 Author Solution Share Posted May 5, 2013 SOLVED Had to use $.parseJSON first Quote Link to comment https://forums.phpfreaks.com/topic/277637-getjson/#findComment-1428309 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.