Question

I have another question similar to a question I asked here: JS Ajax calling PHP and getting ajax call data

However this time I'm dealing with the PUT verb. I was reading to get put data in php should use the following:

file_get_contents("php://input")

Resource: http://www.lornajane.net/posts/2008/accessing-incoming-put-data-from-php

However for put this dosen't seem to be working. I feel like it might have something to do with IIS 7 possibly removing the data?.. I had webDav installed and had to remove that to get the put verb to resolve was wondering is there something else in IIS that could be preventing the data from being parsed at the server level?

My ajax request looks like the following

                var data = '{"storyId":"2","storyName":"a Changed Story.","authorId":"5", "published":"1"}'; 

                $.ajax({
                    type: "PUT",
                    url: BaseUrl + "Story/2",   
                    data: data,
                    success: function(data){
                        console.log(data);
                    },
                    error: function(request){
                        console.log(request);
                    },
                });
Was it helpful?

Solution 2

WooHoo I figured it out thanks to this question.

PHP get PUT request body

For some reason if you request data for "PUT" like this

file_get_contents("php://input")

On the first read it will get all the data. However the second attempt to get data using it like this will return null. This is not the case with POST. I simply had to make it so that we only request the data like the above once.

MY Theory (could be BS) ; ) I think the way it reads PUT request is like reading a file or a stream if there is a way to set the reader to the start again probably could get that data. This is just my theory on how this works I don't really have any resources backing that this works like that.

OTHER TIPS

PUT and DELETE via $.ajax are not supported across all browsers. POST and GET are the only two that are supported by all browsers.

For reference, check the jQuery documentation on `$.ajax here: http://api.jquery.com/jQuery.ajax/

Additionally, you need to change your HTTP daemon's setup to allow your script to access put. This is done by adding the following to your root .htaccess:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ handler.php?uri=$1 [L,QSA]

Note that you must have access to mod_rewite to make these modifications.

See php's documentation on PUT here: http://php.net/manual/en/features.file-upload.put-method.php

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top