Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在serversite上使用javascript和php执行ajax jquery http请求?_Javascript_Php_Jquery_Html_Ajax - Fatal编程技术网

如何在serversite上使用javascript和php执行ajax jquery http请求?

如何在serversite上使用javascript和php执行ajax jquery http请求?,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,嗨,我有一个带有html、css和javascript的Web应用程序。我使用引导和jQuery。我有一个客户端和一个服务器站点 在我的客户端站点上,我有一个index.html: <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script src="https://ajax.goo

嗨,我有一个带有html、css和javascript的Web应用程序。我使用引导和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>
我的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);

您的脚本应该如下所示:

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

检查是否设置了PATH_INFO,因为这会触发PHP通知,导致json字符串响应无效。您还可以使用
错误报告(E_ALL&~E_NOTICE)以隐藏通知。看


你在哪里卡住了?我在我的phpi中创建了一个json想要显示来自phpDo的这个示例数据你看到在你的浏览器控制台中触发了什么吗?
注意:未定义的索引:C:\xampp\htdocs\server\server.php中的路径信息在第14行
[{“Id”:1,“Vorname”:“Max”,“Nachname”:“Mustermann”,“Geburtstag”:“11.11.1980”,“Abteilung”:“Personal”},{“Id”:2,“Vorname”:“Sabine”,“Nachname”:“Musterfrau”,“Geburtstag”:“05.12.1989”,“Abteilung”:“Finanzen”}]问题中显示的URL并非根据定义错误。
$(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");
        }
    });
});
<?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;
    }
}