[OpenLayers-Users] Need Help with OpenLayers.Request

Bill Thoen bthoen at gisnet.com
Sun Jan 11 14:47:31 EST 2009


Can somebody explain to me how to pass data to my server side program 
using OpenLayers.Request? The following example seems to work, only 
there are no variables in the $_POST array. My handler prints only:

result is $_POST variables

But apparently the array is empty.  What am I missing here?

Here's some sample code that illustrates the problem:

<!-- test_ajax.html -->
<html>
  <head>
    <title>AJAX Test</title>
    <script src="http://openlayers.org/api/OpenLayers.js" 
type="text/javascript"></script>
    <script type="text/javascript">

      function init() {
        var XHR = OpenLayers.Request.POST ({
          url: "test_ajax.php",
          data: "my data",
          params: {var1: 'value 1', var2 : 'value 2'},
          headers: { "Content-Type": "text/plain" },
          callback: myhandler
        });
      }

      function myhandler(XHR) {
        alert("result is " + XHR.responseText);
      }

    </script>
  </head>
  <body onload="init();">
    <h1>AJAX Test</h1>
  </body>
</html>


// ---------------------
// test_ajax.php

<?php

  echo "\$_POST variables \n";

  foreach($_POST as $key => $value){
    echo $key.": ".$value;
  }
?>





More information about the Users mailing list