1

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

Bikee
  • 1,197
  • 8
  • 21
John
  • 31
  • 7

2 Answers2

1

well you still need to see the return data, thats why you should console.log the data back. the callback data is not the same scope as the data that you stringify. If you output the data out prior to your alert, it will tell you what is your API response to your request.

azngunit81
  • 1,574
  • 2
  • 20
  • 38
  • I already excludes the input parameter "data" of success and error functions. I just want alert the success and the failure. But in this case, when the success occurs the ajax method alerts failure wrong. The status code returned on AJAX is 0. Thanks – John Apr 19 '16 at 14:35
  • @john what you don't understand is that we are trying to get more information on the debug of your ajax. The debug happens in two places: the data returned and the debug console. Maybe your API is throwing 500 when the ajax is called, maybe the ajax itself is tossing a 500 without you know. All those information helps us debug your code. – azngunit81 Apr 19 '16 at 15:02
  • The problem is that I can't debug the ajax with the console.log, i just can do alert. – John Apr 19 '16 at 15:15
  • @John then open up firebug or google dev console and check if it at least doesn't return 500. Another way to check is to have POSTMAN do the ajax call because POSTMAN acts like your API client, which will mimic your ajax call – azngunit81 Apr 19 '16 at 15:19
  • But i install the http live headers, and you rigth the server (WS) return the code 500 – John Apr 19 '16 at 15:21
  • What can i do to change this situation? Any suggestion? Thanks – John Apr 19 '16 at 15:22
  • Sorry, i had a problem in my server, and momentaneously the response was 500, but now the response given is 200 OK. But Ajax still return the error function. – John Apr 19 '16 at 15:38
0

I finally can get the solution. I want to thank to everybody who tried to help me, the simple solution is to add in the first line of PHP code the following:

header('Access-Control-Allow-Origin: *');

And it will work, at least works for me.

John
  • 31
  • 7