0

So i am sending a JSON response from my controller as:

"date":["07%2F10%2F2013","07%2F16%2F2013","07%2F25%2F2013"]

The dates are being encoded on PHP using urlencode($date);

On my jquery response i read it this way:

var dates = "+this.date+"; 
dates = decodeURIComponent(dates);
var unavailableDates = dates.split(',');

And when printed with console.log i get (using firebug to debug):

["07/10/2013", "07/16/2013", "07/25/2013"]

Now I do it this way:

var dates = '07%2F10%2F2013,07%2F16%2F2013,07%2F25%2F2013';
dates = decodeURIComponent(dates);
var unavailableDates = dates.split(',');

Get the exact same result:

["07/10/2013", "07/16/2013", "07/25/2013"]

But when trying to use it on the jquery datepicker as part of dates that are not available only the bottom portion of code works. Anyone knows why?

Hard Fitness
  • 452
  • 3
  • 9
  • 24

1 Answers1

0

This is more a comment than an answer, because I have no idea what's causing the problem. But I can't do code formatting in a comment, so here goes...

Is there some reason you have to URL-encode your JSON data? That is not something you would normally do in a JSON response. Just let PHP's json_encode() function worry about what needs special encoding.

Without the encoding, your JSON response (or partial response) would look like this:

"date":["07-10-2013","07-16-2013","07-25-2013"]

which is exactly the format you're looking for. This entire block of code becomes unnecessary:

var dates = "+this.date+"; 
dates = decodeURIComponent(dates);
var unavailableDates = dates.split(',');

because this.date is already exactly what you want in unavailableDates.

Now about your question: There's no obvious reason that one of your date arrays would work and the other wouldn't. if you want to check for special characters, you could put both versions in your code—call the final output arrays d1 and d2 and compare them:

if( d1[0] !== d2[0]  ||  d1[1] !== d2[1]  ||  d1[2] !== d2[2] ) {
    debugger;  // stop to investigate the situation
}

But I would start by getting rid of the URL encoding. It can't be doing you any good and only serves to complicate things.

Michael Geary
  • 28,450
  • 9
  • 65
  • 75
  • The JSON return the dates like this date":["07\/10\/2013","07\/16\/2013","07\/25\/2013"] So i decided to url encode to get rid of any odd escaping characters and dealing with the jquery/javascript drama of backslashes. So initially it wasn't url encoded but since it wasn't working tried to url encode it and still nothing. – Hard Fitness Jun 14 '13 at 22:30
  • And i did do a character lenght comparison and array comparison with both unavailableDates and got same length on both of them. Even the string dates are exactly same length. – Hard Fitness Jun 14 '13 at 22:33
  • OK, so you definitely should get rid of the escaping and unescaping code. There's nothing wrong with the backslashes in the string you mentioned. That's just normal JSON encoding. Whatever prevented your code from working, it wasn't these escaped slashes - unless you were using some kind of very unusual and non-standard JSON parsing. If you are using jQuery, the backslashes are not a problem. So a good place to start is by getting rid of all this code, since it isn't helping solve the problem at all. – Michael Geary Jun 15 '13 at 03:41
  • That was the first thing i did was just user the regular JSON parsing, once i saw it wasn't working that is when I tried to use the urlencode to perhaps get it to work. As of now still looking for an answer, no idea why it won't work. – Hard Fitness Jun 17 '13 at 15:44
  • Yeah, I wasn't expecting that removing the encode/decode would fix anything, just that unneeded code can obscure the real problem. At this point my suggestion is to make either a public test page or a [fiddle](http://jsfiddle.net/) that reproduces the bug. I know you are using a server JSON resonse, but jsFiddle has an [echo service](http://doc.jsfiddle.net/use/echo.html) you can use. There's a good chance that just by going through the steps needed to make a public test page or fiddle, you may discover the problem. Or if not, then this will enable others to look at it. – Michael Geary Jun 17 '13 at 21:52
  • Its a bit more complex than that, i am using a AJAX response within a response and creating multiple pieces of code on a table so each single one of those elements when clicked has a functionality. I will be testing this again tomorrow, was able to see it working a bit. But now seems like all items get the same dates shadowed on datepicker. – Hard Fitness Jun 17 '13 at 22:39