1

I am trying to make a form that consumes an API to store information in my Database.

But the file app.php it's not returning anything. There is where I handle to communicate the API with the DB.

I am getting this error in the Developer Extension in my frontend form:

SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data

Just for testing I tried to do var_dump($response); die; in app.php and it shows me the text correctly.

My app.php

<?php

namespace App;

require 'autoload.php';

if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_REQUEST['action'])) {
    switch ($_REQUEST['action']) {
        case 'set':
            $data = new Data;
            $data->name  = $_REQUEST['name'];
            $data->email = $_REQUEST['email'];
            $data->phone = $_REQUEST['phone'];

            header('Content-Type: application/json');
            $response['id'] = $data->save();

            return json_encode($response);
            break;

        default:
            header("HTTP/1.0 404 Not Found");
            break;
    }
} else {
    header("HTTP/1.0 404 Not Found");
}

My app.js file:

    var data = {
        name: name,
        email: email,
        phone: phone,
        action: 'set'
    };

    var sentData = sendData(data);

    if(sentData) {
        alert("OK");
        resetFields();
    } else {
        alert("NOT OK");
    }

function sendData(data) {
    $.ajax({
        url: 'app/app.php',
        method: 'POST',
        data: data,
        dataType: 'json',
        success: function(response) {
            return response;
        },
        error: function(xhr, status, error) {
            return xhr.responseText
        }
    })
}

So, where is the error?

5
  • You have a syntax error, which wont return json. Commented Jan 11, 2018 at 1:07
  • So... where is it? Commented Jan 11, 2018 at 1:08
  • You can see from above header('Content-Type: application/json); Commented Jan 11, 2018 at 1:09
  • 1
    You should also use exit(json_encode($response)); not return json_encode($response); Commented Jan 11, 2018 at 1:11
  • The first error you saw wasn't that, but the exit() helped. You can post it as an answer. Commented Jan 11, 2018 at 1:13

1 Answer 1

1

Instead of using return, you should echo out your json, if it's the end of the script execution you can use exit.

<?php

namespace App;

require 'autoload.php';

if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_REQUEST['action'])) {
    switch ($_REQUEST['action']) {
        case 'set':
            $data = new Data;
            $data->name  = $_REQUEST['name'];
            $data->email = $_REQUEST['email'];
            $data->phone = $_REQUEST['phone'];

            header('Content-Type: application/json');
            $response['id'] = $data->save();

            exit(json_encode($response));
            break;

        default:
            header("HTTP/1.0 404 Not Found");
            break;
    }
} else {
    header("HTTP/1.0 404 Not Found");
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.