1
I am creating an application in REST format in which my front-end requests a PUT (By ajax) to the back-end.
No request I am sending the new information for update, but I am not able to access this information in PHP,
I did a research on the subject, I know that PHP has not defined the super global $_PUT as the $_POST.
I came across people searching the data from php://input
but when I perform the procedure parse_str(file_get_contents('php://input'), $teste);
the return does not come in Array. Has anyone ever gone through this and can give me a light?
Note: I am not using backend frameworks.
PHP code I have a file of routes that calls the method.
public static function update($param){
parse_str(file_get_contents('php://input'), $teste);
echo "<pre>";
var_dump($teste);
exit;
}
Javascript code
Method for making PUT calls
function putAjax(uri, data, callback) {
$.ajax({
url: uri,
type: "PUT",
dataType: "json",
data: data,
cache: false,
contentType: false,
processData: false
}).done(function (data) {
if (callback) {
for (var i = 0; i < callback.length; i++) {
callback[i](data);
}
}
}).fail(function () {
fechaLoading();
return false;
});
}
Method that is calling the putAjax
function updateServidores(form) {
var verificaRetorno = function (data) {
if (data.status) {
if (data.status === true) {
servidor.splice(indiceUpdateServidores, 1);
servidor.push(data.data[0]);
montaGrid(".tableServidores");
abreTela("#viewServidores");
notify("success", "Sucesso!", "Item alterado com sucesso!");
}else {
notify("error", "Erro!", data.erro.message);
}
indiceUpdateServidores = null;
}
};
after = [];
after.push(verificaRetorno);
var formData = new FormData(form);
putAjax(url + "servidor/" + servidor[indiceUpdateServidores].ID_SERVIDORES, formData, after);
}
Exit from file_get_contents('php://input')
:
------WebKitFormBoundarynQKeRELA6A6uJMkr
Content-Disposition: form-data; name="nome"
deletaServidor(this);
------WebKitFormBoundarynQKeRELA6A6uJMkr
Content-Disposition: form-data; name="servidor"
deletaServidor(this);
------WebKitFormBoundarynQKeRELA6A6uJMkr
Content-Disposition: form-data; name="porta"
deletaServidor(this);
------WebKitFormBoundarynQKeRELA6A6uJMkr
Content-Disposition: form-data; name="email"
deletaServidor(this);
------WebKitFormBoundarynQKeRELA6A6uJMkr
Content-Disposition: form-data; name="senha"
deletaServidor(this);
------WebKitFormBoundarynQKeRELA6A6uJMkr
Content-Disposition: form-data; name="email_envio"
deletaServidor(this);
------WebKitFormBoundarynQKeRELA6A6uJMkr
Content-Disposition: form-data; name="reply"
deletaServidor(this);
------WebKitFormBoundarynQKeRELA6A6uJMkr
Content-Disposition: form-data; name="alias"
deletaServidor(this);
------WebKitFormBoundarynQKeRELA6A6uJMkr--
put the code there to know what you’ve done
– Wees Smith
@Weessmith I added the code to the text.
– Lucas Junior
@Lucasjunior take a look at the link that Luizfelipe spoke there
– Wees Smith
@Luizfelipe and @Weessmith I tested the described solution unsuccessfully. I am receiving an array with a single string with all data from
php://input
– Lucas Junior
The question talks about getting data from PUT, but in fact the question is about how to parse the form (and in the comments of the answer that speaks in JSON it was commented that the solution was to change the format of the data, no longer reflecting the problem exposed). In this way, I do not see how the question and answer can serve other visitors, so it was closed.
– Bacco