2

I am writing a web front-end to an existing REST interface which I developed. So far, all my POST and GET requests have been working fine, but when I started adding a PUT request, now I have trouble.

I boiled this down to a very simple test case with 2 files:

My javascript AJAX:

$.ajax({
    url: "putTest.php",
    type: 'PUT',
    dataType: 'application/json',
    data: { testVar: "test" },
    contentType: 'application/json',
    success: function( data ) {

    },
    failure: function( data ) {

    }
});

And my PHP page "putTest.php":

<?php

    var_dump($_SERVER);

?>

I would expect, like in a GET or POST, that my QUERY_STRING server variable would include testVar which is the data I passed to it. However, that is not true, look at the response:

["REQUEST_METHOD"]=> string(3) "PUT"
["QUERY_STRING"]=> string(0) ""
["REQUEST_URI"]=>  string(25) "/test/putTest.php"

What is puzzling me is that I used a Firefox plugin (HttpRequester) and sent the exact same parameters and settings and the response came back perfectly as expected.

Why won't the parameters come across when using the jQuery AJAX?

Thanks!

Kivak Wolf
  • 830
  • 8
  • 29
  • see if this Q&A helps http://stackoverflow.com/questions/8032938/jquery-ajax-put-with-parameters and http://stackoverflow.com/questions/13056810/how-to-implement-a-put-call-with-json-data-using-ajax-and-jquery and http://stackoverflow.com/questions/1749272/jquery-how-to-put-json-via-ajax – Funk Forty Niner Dec 16 '15 at 16:19
  • wow... very interesting. Basically, PUT and DELETE don't accept data, so i would have to add it to the URL (similar to a GET request). Is that correct? – Kivak Wolf Dec 16 '15 at 16:24
  • @KivakWolf No just check my answer – E_p Dec 16 '15 at 16:25
  • To be perfectly honest, I don't know but you could try it. I like to help out by finding links to related questions when I am sometimes unable to fully provide a solution. – Funk Forty Niner Dec 16 '15 at 16:25
  • 2
    It shouldn't have data in QUERY_STRING. PUT, like POST puts the data into the body. – Robert McKee Dec 16 '15 at 16:28

1 Answers1

3

By default php does not support DELETE/PUT ....

<?php
function getRequestParams() {
    if($_SERVER['REQUEST_METHOD'] === "GET" || $_SERVER['REQUEST_METHOD'] === "POST") {
        return $_REQUEST;
    } else {
        // For urlencode
        parse_str(file_get_contents("php://input"), $var);
        // or for json content
        // $var = json_decode(file_get_contents("php://input"));

        return $var;
    }
}

$params = getRequestParams(); 

$params would have your submitted params

Way you parse would depend on dataType you submit with.

E_p
  • 3,136
  • 16
  • 28