0

hi i have a webapplication with html,css and javascript. I use Bootstrap and jQuery. I have a client and a server site.

On my Client site I have a index.html:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.js"></script>
    <script src="js/script.js"></script>
</head>
<body>
    <button type="button" id="add">Mitarbeiter hinzufügen</button>
    <div id="TableContent"></div>
</body>
</html>

here my js:

$(document).ready(function() {
    // Fill table with data from server on first load
    $.ajax({
        type: "GET",
        url: "../server/server.php",
        data: {
            method: "all"
        },
        success: function(content) {
            // Create table content from server data
            var table = $.makeTable($.parseJSON(content));
            // Append table data to table element in index.html
            $(table).appendTo("#TableContent");
        }
    });

my php:

<?php
// partly from
// gonzalo123.wordpress.com/2010/01/09/building-a-simple-http-client-with-php-a-rest-client

class Request
{
    public $url_elements;
    public $methode;
    public $parameters;

    public function __construct()
    {
        $this->methode = $_SERVER['REQUEST_METHOD'];
        $this->url_elements = explode('/', $_SERVER['PATH_INFO']);
        // get GET/DELETE or POST/PUT parameters
        $parameters = array();
        if (isset($_SERVER['QUERY_STRING'])) {  // get GET/DELETE parameters
            parse_str($_SERVER['QUERY_STRING'], $parameters);
        }
        $body = file_get_contents("php://input"); // get POST/PUT request body
        parse_str($body, $postvars);
        foreach ($postvars as $field => $value) {
            $parameters[$field] = $value; // overwrite GET/DELETE parameteres
        }

        $this->parameters = $parameters;
    }
}


class RequestHandler
{

    public function getAction($request)
    {
        $data = $request->parameters;

        // It's only an example code of how to return data from server
        // You need to read information about users from a file data.json
        switch ($request->parameters['method']) {
            case 'all':

                // Create an object of a standard class and add custom
                // variables like Id, Vorname, Nachname and so on
                $person1 = new stdClass;
                $person1->Id = 1;
                $person1->Vorname = "Max";
                $person1->Nachname = "Mustermann";
                $person1->Geburtstag = "11.11.1980";
                $person1->Abteilung = "Personal";

                $person2 = new stdClass;
                $person2->Id = 2;
                $person2->Vorname = "Sabine";
                $person2->Nachname = "Musterfrau";
                $person2->Geburtstag = "05.12.1989";
                $person2->Abteilung = "Finanzen";

                // Add person in array
                $persons = array();
                array_push($persons, $person1);
                array_push($persons, $person2);

                // Encode array to json string and return to client
                return json_encode($persons);
                break;
            case 'single_user':
                break;
            default: // do nothing, this is not a supported action
                break;
        }
        return json_encode($data);
    }


    public function deleteAction($request)
    {
        $data = $request->parameters;
        return json_encode($data);
    }

    public function postAction($request)
    {
        $data = $request->parameters;
        return json_encode($data);
    }

    public function putAction($request)
    {
        $data = $request->parameters;
        return json_encode($data);
    }
}

$request = new Request();
$handler = new RequestHandler();
// tricky: construct call methode depending on HTTP-request-method, e.g. "postAction"
$action = strtolower($request->methode) . 'Action';
$result = $handler->$action($request);
print_r($result);
7
  • Where do you get stuck? Commented Jul 8, 2015 at 17:41
  • i create a json in my php Commented Jul 8, 2015 at 17:42
  • i want display this example data from the php Commented Jul 8, 2015 at 17:42
  • Do you see anything triggered in your browser console? Commented Jul 8, 2015 at 17:43
  • <br /> <b>Notice</b>: Undefined index: PATH_INFO in <b>C:\xampp\htdocs\server\server.php</b> on line <b>14 </b><br /> [{"Id":1,"Vorname":"Max","Nachname":"Mustermann","Geburtstag":"11.11.1980","Abteilung":"Personal"},{"Id" :2,"Vorname":"Sabine","Nachname":"Musterfrau","Geburtstag":"05.12.1989","Abteilung":"Finanzen"}] Commented Jul 8, 2015 at 17:52

2 Answers 2

1

Check if PATH_INFO is set as this triggers a PHP notice causing invalid json string response. You can also use error_reporting(E_ALL & ~E_NOTICE); to hide notices. See http://php.net/manual/en/function.error-reporting.php

<?php
// partly from
// gonzalo123.wordpress.com/2010/01/09/building-a-simple-http-client-with-php-a-rest-client

class Request
{
    public $url_elements;
    public $methode;
    public $parameters;

    public function __construct()
    {
        $this->methode = $_SERVER['REQUEST_METHOD'];
        if(isset($_SERVER['PATH_INFO'])) $this->url_elements = explode('/', $_SERVER['PATH_INFO']);
        else $this->url_elements = array();
        // get GET/DELETE or POST/PUT parameters
        $parameters = array();
        if (isset($_SERVER['QUERY_STRING'])) {  // get GET/DELETE parameters
            parse_str($_SERVER['QUERY_STRING'], $parameters);
        }
        $body = file_get_contents("php://input"); // get POST/PUT request body
        parse_str($body, $postvars);
        foreach ($postvars as $field => $value) {
            $parameters[$field] = $value; // overwrite GET/DELETE parameteres
        }

        $this->parameters = $parameters;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Your script should look like this:

$(document).ready(function() {
    $.ajax({
        type: "GET",
        url: "../server/server.php",// adjust this to be a valid relative URL or an absolute URL
        data: {
            method: "all"
        },
        dataType: "json",
        success: function(content) {
            var table = $.makeTable($.parseJSON(content));
            $(table).appendTo("#TableContent");
        }
    });
});

1 Comment

The URL as showed in the question is not per definition wrong.

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.