jQuery ajax call doesn't call my php page

840 Views Asked by At

I'm implementing Mozilla's persona authentication (ie browserid). So, I have the following onLogin():

function onLogin(pAssertion) {
    var sPHPSessionID = $.cookies.get("PHPSESSID")+'';
    var sFoolCache = new Date().getTime() + '' + Math.random();
    $.ajax(
      {
        cache: false,
        type: 'POST',
        url: '/webservice.php?id=personaauth&u=i',
        data: { assertion: pAssertion, PHPSESSID: sPHPSessionID, z: sFoolCache },
        success: function(res, status, xhr) { alert("do reload");  },
        error: function(xhr, status, err) {
            navigator.id.logout(); 
            alert("Login failure: " + err); 
      }
  });
}

Where there's the "alert("do reload")" I normally have a window.location.reload() call. The problem is that, nonetheless all my efforts to clear and not use the browser cache (Firefox), my /webservice.php page doesn't get called, at all. The ajax call immediately executes the "success" function (which keeps my window reloading in a dramatic loop, if I leave the "reload()" call in the code). I need the PHP session id in my /webservice.php call, so I pass the PHPSESSIONID cookie to the page.

I cleared my firefox cache. I closed my browser, I added the 'sFoolCache' variable to the url (and not the post) but it didn't work either. I'm lost.

3

There are 3 best solutions below

0
On

Looks like there's problem with passing URL, try passing the full URL:

function onLogin(pAssertion) {
    var sPHPSessionID = $.cookies.get("PHPSESSID")+'';
    var sFoolCache = new Date().getTime() + '' + Math.random();
    $.ajax(
        {
             cache: false,
             type: 'POST',
             url: 'FullURL and querystring',
             data: { assertion: pAssertion, PHPSESSID: sPHPSessionID, z: sFoolCache },
             success: function(res, status, xhr) { alert("do reload");  },
             error: function(xhr, status, err) {
                 navigator.id.logout(); 
                 alert("Login failure: " + err); 
             }
        });
}
1
On

As it appeared to help in this case, I'm undeleting this answer

Check here: http://api.jquery.com/jquery.ajax/

Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are deprecated as of jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.

Use this instead:

$.ajax(/* ... */)
.done(function(data) { /* ... */ })
.failure(function(data) { /* ... */ });
0
On

So, Thanks to @Michal, here's how I rewrote it to make it work (btw, I had to answer myself, as my comment was otherwise too long):

function onLogin(pAssertion) {
  var sPHPSessionID = $.cookies.get("PHPSESSID")+'';
  var sFoolCache = new Date().getTime() + '' + Math.random();
  $.ajax(
    {
      cache: false,
      type: 'POST',
      url: '/webservice.php?id=personaauth&u=i',
      data: { assertion: pAssertion, PHPSESSID: sPHPSessionID, z: sFoolCache }
    }).done(function(res, status, xhr) { 
        window.location.reload();
        }).fail( function(xhr, status, err) {
          navigator.id.logout(); 
          alert("Login failure: " + err); 
        });
}

Please vote for Michal's answer (up here), not this one ;-) Thanks guys.