抱歉,如果这已经被问了很多次,但我不能让它工作。我正在尝试建立一个restful风格的网站,我有一个简单的表单:
<form action="/my/path" method="post" id="myformid">
Name <input type="text" name="name">
<input type="submit" value="Test">
</form>我使用Javascript转换用户输入的数据,并使用Ajax将它们发送到我的php文件:
function postData() {
    $('#myformid').on('submit', function(event) {
        event.preventDefault();
        const json = JSON.stringify($("#myformid").serializeArray());
        $.ajax({
            type: "POST",
            url: "/my/path",
            data: json,
            success: function(){},
            dataType: "json",
            contentType : "application/json"
        });
    });
}我尝试在php上读取数据,如下所示:
$data = json_decode(file_get_contents('php://input'), true);
$name = $data["name"];如果我使用Postman这样的工具在请求正文中发送一个JSON,那么代码就可以完美地工作,但是从我使用Ajax测试的结果来看,json数据作为POST数据到达,我可以使用$_POST"name“来读取它,但不能像我那样使用'php://input‘。
我如何修复它,使JSON被接受,甚至通过Javascript发送?
https://stackoverflow.com/questions/56222590
复制相似问题