0
I implemented a WS Rest in PHP, but when I try to consume WS using an Ajax method it always returns the error function. However the WS is saved and the data is even recorded in the BD. Someone has already happened to you the same?
WS code in PHP (This is a simple WS that only saves input data to a txt file):
<?php
require 'vendor/autoload.php';
$app=new \Slim\Slim();
$app->post('/gravar_documento', 'gravar_doc');
function gravar_doc(){
$request=Slim\Slim::getInstance()->request();
$data=$request->getbody();
$dados=json_decode($data,true);
$dados=(array)$dados[0];
file_put_contents('test.txt', $dados["teste"]);
$app=Slim\Slim::getInstance();
$response = $app->response();
$response['Content-Type'] = 'application/json';
$response->status(200);
$response->body(json_encode((object)array('success'=>true)));
}
$app->run();
Ajax method implemented:
$.ajax({
type:"POST",
url:"http://Endereço_foi_ocultado->questões de segurança, mas está bem (pois já executei este WS recorrendo ao SoapUI e não houveram problemas)",
data: teste_enc,
success: function(response, b, c){
alert("Entrou");
},
error: function(response,b,c){
alert("ERRO: "+response.status);
}
});
Again I remind that everything is executed and values are saved in the txt file, only the application returns me the error Alert of the Ajax error function.
Thank you