0

I have been following the answer found in this post. But it only works when passing a local URL, not any external ones.

How do I get it to work with external URLs?

I have the below basic site I have been using for testing.

<!doctype html>
<html lang="en">
<head>
    <title>Test</title>
    <meta charset="utf-8">

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script type="text/javascript">
    function setGoogleToRed ( )
    {
        $('#google').removeClass('label-success').addClass('label-danger');
        setTimeout ( "setGoogleToGreen()", 2000 );
    }
    function setGoogleToYellow ( )
    {
        $('#google').removeClass('label-success').addClass('label-warning');
        setTimeout ( "setGoogleToGreen()", 2000 );
    }
    function setGoogleToGreen ( )
    {
        $('#google').removeClass('label-danger').addClass('label-success');
    }
    function checkurl ( )
    {
      var url = "blah";
      $.ajax(url,
      {
         statusCode: {
         200: function() {
            setGoogleToGreen();
         }
      }, error: function () {
            setGoogleToRed()

        }, 
    });  
    }
    </script>
    <style>
    .label {
        border: 1px solid #000;
    }
    .label-success {
        background-color: #5cb85c;
    }
    .label-warning {
        background-color: #f0ad4e;
    }
    .label-danger {
        background-color: #d9534f;
    }
    </style>

</head>
<body>
<header>
<input type="button" value="Check URL" onclick="checkurl()"/>
<span id="google" class="label label-success" >google</span>
</header>
</body>
</html>
Community
  • 1
  • 1
cashman04
  • 1,134
  • 2
  • 13
  • 27
  • Damn.. cause that is all WAY over my head... – cashman04 May 10 '14 at 19:23
  • Just spent an hour reading different sites and pages linked to by your link, no closer to understanding how to make cross site work. An example would be wonderful if anyone cares to share one. – cashman04 May 10 '14 at 20:55
  • There are several examples on the duplicate question. – Quentin May 10 '14 at 21:20
  • I have tried a few. I am using sinatra, and set up a redirect, but that always returns a 302 from the redir page, not from the page I am actually redirecting too. The anyorigin won't work since the pages I am trying to hit are internal to my network only. And I just don't have a clue how to adapt the CORS method for me, since my whole point is success and error codes, that doesn't seem to have an example for that. – cashman04 May 10 '14 at 21:28
  • XHR should handle the redirect transparently, so you should never see it (but still won't be able to read data from the other origin). The CORS method gives you access to *all* the data, so look at the status instead of the responseText. – Quentin May 10 '14 at 21:33
  • I guess I just don't fundamentally know what the CORS example is doing. I see that it is doing a get on stackoverflow.com...but I don't see what it is doing with the data, or how/where to get the status from it. – cashman04 May 10 '14 at 22:01
  • You'd get the status code in the same way you are doing it in the code in your question. – Quentin May 10 '14 at 22:33

1 Answers1

0

I found a way to make this work with sinatra:

get 'redir/:redir' do
   redir = params[:redir]
   blah = `curl -m 1 -s -0 /dev/null -w %{http_code} "#{redir}"`
   status blah.to_i
end

This allows me to use the code I have above, and pass the url as "/redir/google.com" and then my handler curls the response code, and sets its own status as the status from the page I actually curled. This is extremely dirty, but it fits my needs.

cashman04
  • 1,134
  • 2
  • 13
  • 27