我如何做一个ajax jquery http请求与javascript和php在服务器站点上


how I can do a ajax jquery http request with javascript and php on serversite?

嗨,我有一个web应用程序与html,css和javascript。我使用Bootstrap和jQuery。我有一个客户端和一个服务器站点。

在我的客户端网站我有一个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);

检查是否设置了PATH_INFO,因为这会触发PHP通知,导致无效的json字符串响应。您还可以使用error_reporting(E_ALL & ~E_NOTICE);来隐藏通知。见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;
    }
}

你的脚本应该是这样的:

$(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");
        }
    });
});