Search code examples
javascriptparsingprototypejsremote-access

Is it possible to parse a remote file with ajax/javascript prototype


I want to solve this puzzle but the file resides on remote server. How can I parse this file because I keep getting this error.

XMLHttpRequest cannot load http://www.weebly.com/weebly/publicBackend.php. Origin http://mysite.com is not allowed by Access-Control-Allow-Origin. Refused to get unsafe header "X-JSON"

Code Below

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <title>Untitled Document</title>
        <script type="text/javascript" src="prototype.js"></script>
        <!-- Puzzle starts here -->
<script type='text/javascript'>

  // Note: there may be some unfinished code here, that needs finishing...

  // You should probably try to get this function working...
  function solvePuzzle() {

        new Ajax.Request('http://www.weebly.com/weebly/publicBackend.php', {
          parameters:{
            pos: 'solvepuzzle'
          },
          onSuccess:handlerSolvePuzzle,
          onFailure:function() { alert('Transmission error. Please try again.'); }
        });

  }

  function handlerSolvePuzzle(t) {


        var responseText = t.responseText;
        responseText = responseText.replace(/\n/, "");

        if (responseText.match(/!!$/)) {

          alert("Oops: "+responseText);

        } else {

          // Still need to decode the response

          // Once the response is decoded, we can fire off the alert
          // giving the user further instructions
          //alert(responseText);
          //alert('To complete the challenge, '+t.responseText);

        }

  }

</script>
    </head>
    <body>

<input type="button" onclick="solvePuzzle()" value="hello"/>
    </body>
</html>

Solution

  • Chrome and Firefox's developer tools allow you to modify JS on the fly.

    If you're on Chrome, open up the console by going to the menu View->Developer->JavaScript Console. Copy the js from the page source. Alter it. Then paste altered javascript function(s) into the console. Hit enter. Then start typing 'solvePuzzle();' Hit enter. You'll see the response come back.

    For Firefox, you'll need to download Firebug plugin.