0

我必须研究并介绍如何从网站接收和解析表单数据,但我不确定如何解析数据并将其存储在 Web 服务器上。我的教授向我描述,我应该更多地关注 Web 服务器端。我对我是否应该如何(以及什么)解析这些信息感到有点困惑。

我是否应该获取$_POST['data']PHP 并将其提交到 Web 服务器?没有关于如何接收和解析数据的细节。

如果有人可以帮助我向我解释 REST Web 服务如何解析表单数据,我将不胜感激。

我的教授给我的唯一提示是搜索“申请表编码”。我找不到对此的解释。

4

1 回答 1

0

You could send data to your service by (jquery) AJAX, sending a json , jsonObject in this example:

<script type="text/javascript">
    function send() {
        var jsonObject = {
            name: 'bobby',
            address: 'st. test'
        }

        $.ajax({
            url: '/myRestService.php',
            type: 'post',
            dataType: 'json',
            success: function (data) {
                alert(data)
            },
            data: jsonObject
        });
    }
</script>

and further, in your service (myRestService.php) you receive json, and reply some response, in this example: true, and received name

<?php

//read received JSON (you can receive by $_POST if you want, just change this line)
        $data = file_get_contents("php://input");
//parse JSON to PHP object
        $data = json_decode($data);        

//return your service response
        $jsonResponse = "{ \"response\": true, \"text\": \"Hey, your name is: " .$data->name . "\"}";        
        echo $jsonResponse;
        exit;
}


?>

this is a very basic example how serivces works, and REST services are much more complex than your question, however this simple service example fit in your question because it's how REST services works as well.

于 2013-11-06T23:53:10.577 回答