I've implemented a WS Rest in PHP, but when I try to consume WS with an Ajax method it always returns me the error function. However the WS is recorded and the data is even recorded in the DB. Has anyone ever had the same problem?
PHP WS code (This is a simple WS that only writes the 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 remember that everything is executed and the values are written to the txt file, except that the application returns me the error alert of the Ajax error function.
Thank you