marcj / php-rest-service

Php-Rest-Service is a very simple and fast PHP class for server-side RESTful JSON APIs.
MIT License
216 stars 74 forks source link

always getting bad request 400 performing a POST #28

Closed freinn closed 8 years ago

freinn commented 8 years ago

Hi!

I'm having trouble using this library because I don't know well how to insert POST parameters and then retrieve them on the server.

This is the important part of my PHP file:

 ->addPostRoute('db/(.+)', function($tok) {

    var_dump($tok);

    $myfile = fopen("./newfile.txt", "w") or die("Unable to open file!");
    fwrite($myfile, $tok . "\n");

    die();
  }

The important thing for me is to make this work and know how to access the POST parameters.

And on Javascript, I try to perform AJAX with jQuery this way:

  var objeto = {
    "token":"tokenSano",
    "campo": "contenido"
  };
  var postDATA = JSON.stringify(objeto);

  jQuery.ajax({
    url : "http://192.168.0.191/servicioWebREST/index.php/db",
    type: "POST",
    data : JSON.stringify(objeto),
    crossDomain: true,
    //contentType: "application/json; charset=utf-8",
    dataType: "text",
    // cache: false,
    processData: false,
    // beforeSend: beforeSendHandler,
    success: function(data, textStatus, jqXHR)
    {
      console.log('success');
    },
    error: function (jqXHR, textStatus, errorThrown)
    {
      console.log('failure');
    }
  });

When I send this to the server, it always responds with error 400 Bad Request.

marcj commented 8 years ago

Just use $_POST['tok'] and don't use /(.+).

freinn commented 8 years ago

Thanks, I will make a pull request trying to improve the docs.