I implemented a Rest Service using the PHP framework named Slim
, the service works fine, because i also tested in SoapUI
and works without problems. The Service is very simple, it just saves the data into txt file. Although when i consume the service via javascript using a Ajax post method it works, and the data is saved in the file, but the error function is executed instead the success function. Any ideas, whats going wrong?
Service-> PHP SLIM FRAMEWORK
<?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);
file_put_contents('test.txt', $dados["test"]);
$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
$.ajax({
type:'POST',
url: "Service_url->hidden for safety",
data: JSON.stringify( { test: 1, val: 2} ),
success: function(data){
alert("IN");
},
error: function(data){
alert("Fail");
}
});
Thanks