I’m building a web calendar application and I’m trying to add a feature that checks the db before sending a form to see if we can add the calendar event or not.
Right now I have this script that try to get the response from a PHP file:
// send form data using ajax requests
$.post(
"addons/validate_event.php",
$("#modif_inventaire").serialize(),
function(response){
if (response) {
var htmlresponse = "";
$.each(response, function(key, value) {
htmlresponse += key + ': ' + value;
});
alert(response)
alert(htmlresponse)
}
}
);
The response “addons/validate_event.php” will look like this: {} (empty == success) or like this:
{"20120106":[1300,1430,1510,"1600"],"20120107":["0900",1000,"1100","1200","1300","1400","1500"]}
Basically, I have a PHP script that check if we can add the event, if so, it returns an empty array. Otherwise, it returns an array containing dates in the format yyyymmdd and each date has an array of available hours in this format hhmm.
But my problem is that when i try to iterate the array, alert(response) gives me the php answer {"20120106":[1300,143 .... but alert(htmlresponse) gives me this:
0: {1: "2: 23: 04: 15: 26: 07: 18: 09: 310: "11: :12: [13: "14: 015: 916: 017: 018: "19: ,20: 121: 022: 423: 024: ,25: "26: 127: 128: 029: 030: "31: ,32: 133: 034: 235: 036: ,37: 138: 139: 040: 541: ,42: 143: 044: 245: 546: ,47: 148: 149: 150: 051: ,52: 153: 054: 355: 056: ,57: 158: 159: 160: 561: ,62: 163: 064: 365: 566: ,67:......
What am I missing?
Thanks a lot
Joel
If you want $.get/$.post to parse the JSON response automatically, you’ll need to pass in ‘json’ as the fourth argument, as the following: