jQuery: The Write Less, Do More JavaScript Library

jquery ajax and listening for users exiting the site

View: New views
2 Messages — Rating Filter:   Alert me  

jquery ajax and listening for users exiting the site

by numerical25 () :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I want to send data to a remote site but only after the customer leaves. Its not working and I just want to make sure I am not doing anything wrong. can anyone see anything wrong with the following code

        $(window).bind('beforeunload', function()
        {
                $.post('http://myWebsite.com/usersessions/store', {data: this.exportJSON()});

        });


Below is another attempt

        $(window).bind('beforeunload', function()
        {
                 $.ajax({
                           type: "POST",
                           url: "http://play.anthonylgordon.com/usersessions/store",
                           data: "data='Test'",
                           success: function(msg){
                                 alert( "Data Saved: " + msg );
               
                           }})
       
        });

I am using php for my server side language. cakephp framework. is it possible to find out if it was able to successfully  load. is there some type of techique for debugging these issues. Cause I cant tell rather its not working on the site that's making the request. or the website that retrieving the data.

Re: jquery ajax and listening for users exiting the site

by James-279 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Is http://myWebsite.com the same domain that your beforeunload code is
called on? If not, then it won't work because of cross-domain policies
for AJAX (XMLHttpRequest object).
If so, try removing the full URL so that it's a relative path, e.g. "/
usersessions/store"

Also, make sure that:
    this.exportJSON()
returns the expected content. You might want to just try to console.log
() the data inside that beforeunload function just to check.

On Nov 1, 4:31 pm, numerical25 <numerica...@...> wrote:

> I want to send data to a remote site but only after the customer leaves. Its
> not working and I just want to make sure I am not doing anything wrong. can
> anyone see anything wrong with the collowing code
>
>         $(window).bind('beforeunload', function()
>         {
>                 $.post('http://myWebsite.com/usersessions/store', {data:
> this.exportJSON()});
>
>         });
> --
> View this message in context:http://old.nabble.com/jquery-ajax-and-listening-for-users-exiting-the...
> Sent from the jQuery General Discussion mailing list archive at Nabble.com.