简体   繁体   中英

Receive POST request in PHP from OpenLayers JavaScript

I have problems in receiving POST request in PHP. I'm using JavaScript to send data to a PHP page with POST request. The JavaScript is from OpenLayers.js, and the part that sends the request looks like this:

var postrequest = OpenLayers.Request.POST({
        url: "http://localhost/index.php",
        data: "success",
        headers: {
            "Content-Type": "application/x-www-form-urlencoded"
        }
    });

In PHP, I'm using this code to see, what I'm getting:

<?php
    print_r($_POST);
?>

This is what happens:

  1. index.php receives POST request.
  2. FireBug also informs that POST Parameters contain Success, the one that was sent.
  3. print_r($_POST); in index.php just gives this: array() and doesn't change after the POST request from JavaScript.

So the data is sent and received, but my PHP code doesn't somehow understand it, or I'm not using the right PHP function.

Any suggestions, where to look, and what to try?

I think the "data" property needs to be an object containing key/value pairs.

eg:

var postrequest = OpenLayers.Request.POST({
        url: "http://localhost/index.php",
        data: {
          userName: "myUsername",
          password: "myPassword"
        },
        headers: {
            "Content-Type": "application/x-www-form-urlencoded"
        }
    });

If this works when you print_r($_POST) you should see array("userName" => "myUsername", "password" => "myPassword")

I guess you need include XMLHttpRequest.js library, you can download it from this link

https://github.com/ilinsky/xmlhttprequest

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM