April 3, 2012

Ajax return var with server response

Question by Tomirammstein

I have this function, just make a simple request to the server, simply AJAX.

( function() {
'use strict';
Request  = { 
    Call: function ( u, theme, params ) { 
      var ajax, t; ajax = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP'); 
      ajax.onreadystatechange = function() { 
        if ( ajax.readyState == 4 ) { 
          this.Reponse = ajax.responseText; 
        } 
      }; 
      if ( theme == 'POST' ) { 
        ajax.open('POST', u, true); 
        ajax.setRequestHeader('Content-type','application/x-www-form-urlencoded'); 
        ajax.send(params); 
      } 
      else { 
        if ( theme == 'GET' ) { 
          ajax.open('GET', u, true); 
          ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); 
          ajax.send(null); 
        }  
      } 
    },
    Response : null
  } 
})();

I want to store the server’s response into a variable, like this :

window.onLoad = function () {
  Request.call('post.php', 'POST', 'bar=foo');
  //then
  document.getElementById('foo').innerHTML = Ajax.Request.Response;
}

Because I want to process the response separately.

Somebody can help me?

Answer by Starx

You can access the response using responseText

var response = ajax.responseText
September 21, 2011

How to know route from the URL or $request Object?

Question by mrN

PLOT:

After implementing ACL on my website, if a user tries to access unauthorised page he will be denied and shown a page to login. After he loggs in, I wanted to redirect the user to the previous page to which he was denied to earlier.

To do this, I store the request parameters using $request -> getParams(), onto a session variable, which will be used to generate the url again. This is where the problem occurs, to generate the url back, I need the name of the route and that i dont know how to read.

I need to know the route name, so that I will be able to regenerate the url, from the array stored in session, or if there is a better way to solve this, please suggest.

Answer by Starx

Dont try to think of complex solutions for simple problem.

You can do this, with just using $_SERVER['REQUEST_URI'], this gives the same result as @Phil’s answer (Correct me, If i am missing something). and is more than enough to do what you want.

...

Please fill the form - I will response as fast as I can!