views:

703

answers:

3

I'm making a $.get to call a service 'A'. Service 'A' returns plain text which I display on the page. But sometimes it redirects to service 'B' which returns plain text. But, I'm unable to handle the response text of service 'B'. How do I do that?

A: 

If service B is listening at different domain, then such cross-domain AJAX calls are forbidden, unless there is crossdomain.xml file allowing it.

PanJanek
i'm using jsonp to make cross domain calls ('A' and 'B' return json text in fact)... and 'A' is also on a different domain.
manu1001
Hmm. Have you tried to trace the requests going from the browser?
PanJanek
A: 

I can not prove, but I hope that this script can guide you to a solution:

you would have to prove your status differences or text on each type of response from "a.php"

$.ajax({
  type: "GET",
  url: "a.php",
  complete: function (XMLHttpRequest, textStatus) {
     if (XMLHttpRequest.status!=200) // or responseText 
     { 
       var fn = arguments.callee;
       var _this = this;
       setTimeout(function(){fn.call(_this, XMLHttpRequest, textStatus);}, 200);
     }
     else
     {
       //ok
     }
  }
});

or EDIT:

  complete: function xCompleteFunction(XMLHttpRequest, textStatus) {
     if (XMLHttpRequest.status!=200) // or responseText 
     { 
       var _this = this;
       setTimeout(function(){xCompleteFunction.call(_this, XMLHttpRequest, textStatus);}, 200);
     }
     else
     {
       //ok
     }
  }

function call to itself

EDIT II:

redirect.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt; 
<title></title>
<script type="text/javascript">
$(function(){
    $("#senddata").click(function(){
     $.ajax({
      type: "GET",
      url: "a.php",
      complete: function xCompleteFunction(XMLHttpRequest, textStatus) {
          $("#info").append(""+XMLHttpRequest.status+"<br />"+XMLHttpRequest.responseText+"<br>");
       if (XMLHttpRequest.status==301) // or responseText 
       { 
        var _this = this;
        setTimeout(function(){xCompleteFunction.call(_this, XMLHttpRequest, textStatus);}, 200);
           $("#info").append("waiting redirect<br>");
       }
       else
       {
           $("#info").append("redirect ok<br>");
       }
      }
     });
    });
});
</script>
</head>
<body>
<button id="senddata">send ajax request</button>
<pre id="info"></pre>
</body>
</html>

a.php:

<?php
for($a=0;$a<1000000;$a++)
{
    //wait
}
header('Location: b.php');

b.php:

<?php
    print "hola mundo";

Important: Status Code Definitions

andres descalzo
i tried copying this and using but it didn't seem to work. i can't figure out much. could you please explain this code? i'm completely new to javascript.
manu1001
A: 

What is the nature of Service 'A' and 'B'? A neater solution would be to handle the discrepancy server-side. For example, if Service A is a PHP script...

<?php

if(prerequisite for display service A plain text){

    echo "service a plain text";

}else{

    echo "service b plaint text";

}

?>

Alternatively, if these services are text files, you could have a third file that makes the decision, and includes Service A or B depending. For example, this could be ajax.php, and you would call ajax.php in all cases:

<?php

if(prerequisite for displaying service A plain text){

    echo file_get_contents("a.txt");

}else{

    echo file_get_contents("b.txt");

}

?>

From what it sounds like, your conditional will be file_exists("a.txt"), but that's just a guess on my part.

Good luck, and comment if you've any questions!

Gausie
no, that's not how it works.the application is integrated with CAS, so a request for 'A' causes a redirect to CAS which authenticates and then does another redirect to 'A'.i can see the redirects using firebug but not in the handler in jquery.
manu1001