1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

$.ajax, cookies and callbacks

Discussion in 'jQuery' started by sarahk, Mar 31, 2014.

  1. #1
    I've got a bit of code that runs like this...
    1. User logins in Site A
    2. $.ajax sends the data to Site B
    3. Site B authenticates, sets cookies and returns a 1 char flag
    4. Site A then reloads to the exact same page

    @PoPSiCLe - I saw your other post about reloads but there are too many settings dependent on the login/logout and a complete reload would be cleaner.

    Now, that is all good but something is coming unstuck.

    If I stop at #3 and manually reload the page the login/logout will have worked
    If I automatically reload (#4) the remote script won't have run.

    When I watch the "network" tab on Chrome's inspect panel and have my finger hovering on the printscreen button I can see this in the image

    upload_2014-3-31_20-37-9.png

    I get that a reload would cancel an ajax call but it's in the callback so surely that should be running after there is a response from the remote script?

    Edit: complete seems ok, so I'll use that. Confusing though.
     
    Last edited: Mar 31, 2014
    sarahk, Mar 31, 2014 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #2
    Uhm ... if you are reloading on login, why are you wasting time with AJAX crap?

    Though if that's actually two separate sites, shouldn't that NOT work since that would be a cross site execution?
     
    deathshadow, Mar 31, 2014 IP
  3. AbstractChaos

    AbstractChaos Member

    Messages:
    58
    Likes Received:
    4
    Best Answers:
    4
    Trophy Points:
    25
    #3
    My guess (as you haven't posted any code), is that your refresh is called inline with the ajax (Asynchronous JavaScript and XML) which runs separately to the execution of your code flow.

    Change
    $.ajax('siteb/login')
    //refresh code here
    HTML:
    to
    $.ajax('siteb/login').done(function() {
      //refresh code here
    });
    HTML:
    this will ensure that the request is complete before you kill all requests and refresh page.

    Whilst I answered your question to fix what you have I would have to agree with deathshadow why not use a standard post and do the following.

    1. User logins in Site A
    2. Site A Server then sends request to SiteB which authenticates and returns
    3. Page Comes back all logged in and happy.
     
    AbstractChaos, Mar 31, 2014 IP