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

Asked

Viewed 287 times

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

1 answer

1

Go to your browser’s developer tools, in the Network section, see the response to the ajax request. There must be the reason of execution is giving error. If the data are recorded correctly, the problem is time to generate the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.