1

In my development, I came on a strange problem. The following is my jquery code to load 2 datepicker when the page load, those 2 datepickers are disable the dates which are not available, here is the code:

$(document).ready(function () {

                                $('#textfield1').datepicker( "destroy" );
                                $('#textfield2').datepicker( "destroy" );
                                $("#loading2").html('<img src="images/loading.gif"/>');
                                var dataString = 'a=b';
                                $.ajax({
                                    type: "GET",
                                    url: "include/getdate.php",
                                    data: dataString,
                                    success: function(data){

                                        $(".tempimg").hide();

                                        $("#textfield1hid").datepicker({ 
                                            showOn: "button",
                                            buttonImage: "/images/calendar.gif",
                                            buttonImageOnly: true,
                                            dateFormat: 'd M yy',
                                            altField: "#textfield1",
                                            altFormat: "yy-mm-dd",
                                            beforeShowDay: reservedDates
                                        });
                                        $("#textfield2hid").datepicker({ 
                                            showOn: "button",
                                            buttonImage: "/images/calendar.gif",
                                            buttonImageOnly: true,
                                            dateFormat: 'd M yy',
                                            altField: "#textfield2",
                                            altFormat: "yy-mm-dd",
                                            beforeShowDay: reservedDates
                                        });
                                        natDays = data;
                                        function reservedDates(date) {
                                            for (i = 0; i < natDays.length; i++) {
                                              if (date.getMonth() == natDays[i][0] - 1
                                                  && date.getDate() == natDays[i][1]) {
                                                return [false, natDays[i][2] + '_day'];
                                              }
                                            }
                                          return [true, ''];
                                        }
                                            $("#loading2").html('');
                                        }
                                    });
                                    return false;
                                });

This code is working fine on Firefox and IE7. But when the it shows in IE8, the datepickers are not loading, but the ajax responses are coming from the server. These are my 2 text fields

<input type="text" name="textfield1hid" id="textfield1hid" />
                                    <input type="text" name="textfield2hid" id="textfield2hid" />

This is the response from the server:

[[04,01],[04,01],[04,02],[04,02],[04,03],[04,03]]

After IE8 loaded the page, still the text fields are as shown below

<input name="textfield1hid" disabled="disabled" id="textfield1hid" style="background-color: #a0a0a4;" type="text"/>

the next text field also get the same attributes, and I added them a long time ago and removed them all, other browsers are working fine even IE7 but problem is in IE8

Can anybody give me a help to solve this problem

Mujahid
  • 1,227
  • 5
  • 32
  • 62
  • Could you plz open the 'Developer Tools' (seems to be on F12 shortcut), check an error message on the JavaScript console and post it here? – zindel Apr 05 '11 at 12:09
  • @zindel: There's no Error Message in there :( – Mujahid Apr 05 '11 at 12:10
  • Can you try to add `cache: false` option to the $.ajax function and see if it helps? – bobo Apr 05 '11 at 12:51
  • did u try with `$('#textfield1').attr("disabled", "disabled");` instead of `true` – xkeshav Apr 06 '11 at 06:11
  • yes, the problem throws when it come to the dataType:'json' actually my response from the server is not a json, its just an array, but even i add the dataType'json' firefox and ie7 gives proper result, but ther other browsers fail, pls take a look at my server response, its not a json response – Mujahid Apr 06 '11 at 06:14
  • that is clearly not json string, is that u r getting when use dataType `json` in firefox?? – xkeshav Apr 06 '11 at 06:18

3 Answers3

1

Can you try this:

change dataType to text

and eval the data.

natDays = eval('(' + data + ')');

Zayar
  • 347
  • 2
  • 11
  • 2
    Wow. This is the accepted answer? It works, but it makes me shudder to think that someone is going to use this in production code. – Alex Apr 06 '11 at 06:25
0

I remember having trouble in similar cases in IE when I did not specify what response format I expected. Try setting dataType in the ajax request like so:

$.ajax({
    type: "GET",
    url: "include/getdate.php",
    data: dataString,
    dataType: 'json',
    success: successCallback
}

Also if you want to catch errors you should be able to specify an error callback like so:

$.ajax({
    ....
    error: errorCallback
    ....
}

function errorCallback(jqXHR, textStatus, errorThrown) {
    alert(jqXHR);
    alert(textStatus);
    alert(errorThrown);
}

That should help with debugging.

Jon Nylander
  • 8,743
  • 5
  • 34
  • 45
  • I've already mentioned it as it is, but it didn't work that's why I removed it mate :( – Mujahid Apr 05 '11 at 12:14
  • Added info about how to debug. – Jon Nylander Apr 05 '11 at 12:17
  • Thanks, I'll try it, but one more thing, when I updated the server with edited files, IE8 takes very older files, might be an IE8 cache, I removed all the cache from IE8, but it still takes the older files, any idea how to fix it – Mujahid Apr 05 '11 at 12:22
  • Sounds very weird that it would cache. Do you know what the Content-Type header of the server response is? It should be `application/json`. – Jon Nylander Apr 05 '11 at 12:29
  • now I'm not sending a json from the server, as this php server has older version of PHP (PHP 5.1) json is not working on this server, so I create a JSON like array in the server side and send it to the client side, but this is the header and it takes it as application/json `Accept application/json, text/javascript, */*; q=0.01` – Mujahid Apr 05 '11 at 12:34
  • You can still send json from PHP prior to the inclusion of the json functions. Just make sure that your JSON "string" is formatted correctly, and that the response header is specified as such: `header('Content-type: application/json');` – Jon Nylander Apr 05 '11 at 12:53
  • @Jon Nylander: as you've mentioned I added that errorCallback function, and when the page reloads, 3 popups popped up, the first one says [object Object] and the second one says parsererror and the third one says: SyntaxError:Syntax error. Can you pls tell me how to fix it. thanks – Mujahid Apr 06 '11 at 04:20
  • @Jon Nylander: Please note these 3 popups popped up only in IE8 – Mujahid Apr 06 '11 at 04:22
  • @Jon Nylander: I tried it with Chrome as well, same popup error poping up in chrome as well and its not working too :( – Mujahid Apr 06 '11 at 05:11
0

there are json library

JSON.parse('[{"some":"json"}]');
JSON.stringify([{some:'json'}]);

Reference

Community
  • 1
  • 1
xkeshav
  • 53,360
  • 44
  • 177
  • 245
  • my client's server has older version of PHP and it doesn't support JSON, that's y I had to make an array from the server side, otherwise I could have use $json_encode(), but thanks a lot diEcho :) this will help me next time – Mujahid Apr 06 '11 at 06:26