CodeIgniter Forums
Jquery ajax request works in some pages and in others it doesnt - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Jquery ajax request works in some pages and in others it doesnt (/showthread.php?tid=52820)

Pages: 1 2 3


Jquery ajax request works in some pages and in others it doesnt - El Forum - 06-28-2012

[eluser]iceuser[/eluser]
Code:
$(".view_comments").click(function(){

        var cct = $.cookie('csrf_cookie_name');

        $.ajax({
            type: "POST",
            url: www+"comment/getComments",
            dataType: 'json',
            data: {
                csrf_token_name : cct
            },
            success: function(data) {
                $(".page").html(data.html);
            }
        });

    });

HOw can the above ajax code work in some pages and it doesnt it wont work? This is just crazy!

I have spent seriously HOURS trying to debug it by creating temp pages with just the neccessary html data needed to check if the request is the only problem but its not!

It simply works on user homepage which is http://www.site.com/ and it wont work when a user views a message http://www.site.com/mssages/inbox/12313213 where the 1231313 is the ID of the message...

The above is just for that ajax call some other work all the time and some others simply doesnt...


Jquery ajax request works in some pages and in others it doesnt - El Forum - 06-28-2012

[eluser]vitoco[/eluser]
i can see 2 posible problems :
1.- click doesn't get called, cause "el.view_comments" are created dinamically
Solution : use bind() or live()
2.- json response fails, and error event isn't defined
Solution : add "error: function()" to ajax call and check server side

Code:
$(".view_comments").live( 'click' , function()
    {
        alert('CLICKED');
        var cct = $.cookie('csrf_cookie_name');

        $.ajax({
            type: "POST",
            url: www+"comment/getComments",
            dataType: 'json',
            data: {
                csrf_token_name : cct
            },
            error : function( data )
            {
                alert( "ERROR" );
            },
            success: function(data)
            {
                alert( "SUCCESS" );
                $(".page").html(data.html);
            }
        });

    });

Saludos


Jquery ajax request works in some pages and in others it doesnt - El Forum - 06-28-2012

[eluser]iceuser[/eluser]
[quote author="vitoco" date="1340913665"]i can see 2 posible problems :
1.- click doesn't get called, cause "el.view_comments" are created dinamically
Solution : use bind() or live()
2.- json response fails, and error event isn't defined
Solution : add "error: function()" to ajax call and check server side

Code:
$(".view_comments").live( 'click' , function()
    {
        alert('CLICKED');
        var cct = $.cookie('csrf_cookie_name');

        $.ajax({
            type: "POST",
            url: www+"comment/getComments",
            dataType: 'json',
            data: {
                csrf_token_name : cct
            },
            error : function( data )
            {
                alert( "ERROR" );
            },
            success: function(data)
            {
                alert( "SUCCESS" );
                $(".page").html(data.html);
            }
        });

    });

Saludos[/quote]

I am already using .live method so this doesnt seem to be the problem here...

what happens is that i get the

CLICKED

message

then the ajax request goes for 5-8 minutes and while i check it via Firebug i can see that the loading gif just spings for all the time without getting any error

then finally after 5-8 minutes i see the red color with an "X" image next to the link and as response i get nothing

i then get the

ERROR

message...

The above request works on Safari(i am on an iMac) and on all browsers on Windows and other machines... it seems my iMac is the problem here... i even removed XAMPP as my web server and added MAMP which didnt fix the problem...

next option is to clean install MAC OS X


Jquery ajax request works in some pages and in others it doesnt - El Forum - 06-28-2012

[eluser]iceuser[/eluser]
If i change a bit the error function to:

Code:
error: function(jqXHR, textStatus, errorThrown) {
                console.log( "ERROR" );
                console.log(jqXHR);
                console.log(textStatus);
                console.log(errorThrown);
            },

i get:

Code:
ERROR
Object { readyState=0, status=0, statusText="error"}
error
(an empty string)



Jquery ajax request works in some pages and in others it doesnt - El Forum - 06-28-2012

[eluser]vitoco[/eluser]
i think that you need to debug your server side script.


Jquery ajax request works in some pages and in others it doesnt - El Forum - 06-28-2012

[eluser]iceuser[/eluser]
[quote author="vitoco" date="1340915019"]i think that you need to debug your server side script.[/quote]

i have spent several hours debugging it client and server side but the problem seems to exist on Firefox and Chrome... as i explained above...

It can't simply work for months and start going crazy for 2 browsers only and only on MAC!

This is insane!!


Jquery ajax request works in some pages and in others it doesnt - El Forum - 06-28-2012

[eluser]Matalina[/eluser]
how are you get www? for your url?


Jquery ajax request works in some pages and in others it doesnt - El Forum - 06-28-2012

[eluser]iceuser[/eluser]
[quote author="Matalina" date="1340918095"]how are you get www? for your url?[/quote]

its a global variable in the javascript file located at the top... its outside
Code:
$(document).ready(function(){ });



Jquery ajax request works in some pages and in others it doesnt - El Forum - 06-28-2012

[eluser]Matalina[/eluser]
and it's just a string?


Jquery ajax request works in some pages and in others it doesnt - El Forum - 06-28-2012

[eluser]iceuser[/eluser]
[quote author="Matalina" date="1340918272"]and it's just a string?[/quote]

yes its like

Code:
var www = "http://www.mysite.dev/";

which is a local host for my website development purposes