title: Cross-origin request blocked when using AJAX
body: <p>Hey I just installed script on same host, twice, just different accounts.
  The scripts worked fine, but for random reason, one of them started to drop all AJAX requests, and print the following messsage in Mozilla: </p>
  
  <blockquote>
    <p>Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at <a href="http://www.yoursite.com/vote/ajax/xmlhttp/claim" rel="nofollow">http://www.yoursite.com/vote/ajax/xmlhttp/claim</a>. This can be fixed by moving the resource to the same domain or enabling CORS.</p>
  </blockquote>
  
  <p>When I access the link directly, works fine, but with AJAX it just fails.. on the other account (host account) it works fine so far.</p>
  
  <p>On my VPS and pc it works fine.</p>
  
  <p>In PHP error log, it prints undefined variables, which is not really possible, how can AJAX affect this, especially in this random situation, where it works and sometimes now on this host.</p>
  
  <p>My JS:</p>
  
  <pre><code>    var reward = $("#rewards ul li");
      var selected = [];
      var limit = &lt;?php echo $limit; ?&gt;;
      var errors = $(".error");
      var timeout;
  
      console.log(limit);
  
      $("#claim").click(function() {
          $("#claim").text('Checking...');
          if ($("#username").val() == "") {
              $("#claim").text('Claim my reward(s) now');
              showError("Fill in your in-game username.");
              return;
          }
          if (selected.length == limit) {
              $.ajax({
                  type: "POST",
                  url: "&lt;?php echo $post_url; ?&gt;",
                  data: {username: $("#username").val(), rewards: JSON.stringify(selected)},
                  success: function(data) {
                      $("#claim").text('Claim my reward(s) now');
                      if (data.substr(0, 6) == 'error:') {
                          showError(data.substr(6));
                      }
                      else {
                          $("#claim").text('Success!');
                          $(".section").html(data);
                      }
                  }
              });
          }
          else {
              showError("You must select " + limit + " rewards(s) to continue.");
          }
      });
  </code></pre>
  
  <p><em>Let me know if you need the functions I used too.</em></p>
  
  <p>What is causing this problem?</p>
  
  <p>I tried adding the following response:</p>
  
  <pre><code>if (isset($_SERVER['HTTP_ORIGIN'])) {
      header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
      header('Access-Control-Allow-Credentials: true');
      header('Access-Control-Max-Age: 86400');    // cache for 1 day
  }
  </code></pre>
  
  <p>But it's bad because it will still have undefined variables in it, and still process the response which will cause JavaScript syntax errors.</p>
  
  <p>Thanks in advice.</p>