Rest Web Service using Slim Framework, in PHP, always gives error when called by an Ajax method, although it is executed?

0

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

    
asked by anonymous 19.04.2016 / 13:06

1 answer

1

Go to the developer tools of your browser, in the section on network, see what the ajax request response is. There must be the reason for running this giving error. If the data is saved correctly, the problem is time to generate the response.

    
27.07.2016 / 04:00