简体   繁体   English

AJAX 对 PHP 服务器的 POST 请求:控制台上的无效请求(格式错误的 HTTP 请求)和 .net::ERR_EMPTY_RESPONSE 上的 chrome 开发工具

[英]AJAX POST request to a PHP server: Invalid request (Malformed HTTP request) on console and net::ERR_EMPTY_RESPONSE on chrome dev tools

I have the files test.html , test.js and test.php in my directory.我的目录中有文件test.htmltest.jstest.php I am trying to send some data to my backend, which should be dealt with by test.php .我正在尝试将一些数据发送到我的后端,这应该由test.php处理。 I run my server using PHP's built-in webserver with the command php -S 0.0.0.0:8080 -t test-server/ .我使用 PHP 的内置网络服务器运行我的服务器,命令php -S 0.0.0.0:8080 -t test-server/

Here's a simplified demonstration, whenever I click "submit", it should send the data to my php server.这是一个简化的演示,每当我点击“提交”时,它应该将数据发送到我的 php 服务器。 The JS:脚本:

window.onload = () => {
    submit = document.getElementById('submit');
    submit.addEventListener('click', () => {
        const xhttp = new XMLHttpRequest();
        xhttp.onload = function() {
            window.location.replace("test.php");
        }
        xhttp.open("test.php", "POST", true);
        xhttp.setRequestHeader('Content-type', 'application/json; charset=utf-8');
        var data = {'name':'foo'};
        xhttp.send(JSON.stringify(data));
    });
}

And here's my PHP:这是我的 PHP:

<?php 
    echo "foo";
    var_dump($_POST);
?>

However, the data isn't being sent, and the xhttp.onload never works.但是,数据没有被发送,并且xhttp.onload永远不会工作。 The error Invalid request (Malformed HTTP request) shows up on my server and the error net::ERR_EMPTY_RESPONSE pops up in chrome's developer tools.错误Invalid request (Malformed HTTP request)出现在我的服务器上,错误net::ERR_EMPTY_RESPONSE弹出在 chrome 的开发人员工具中。

What am I doing wrong?我究竟做错了什么?

The xhttp.open uses this signature: xhttp.open 使用这个签名:

open(method, url, async, user, password)

You just inversed method and url in your call.您只是在调用中反转了方法和 url。

More informations: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/open更多信息: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/open

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Node.js发送请求接收net :: ERR_EMPTY_RESPONSE - Receiving net::ERR_EMPTY_RESPONSE with Nodejs Post Request 如何修复Jquery Ajax Post Request中的“net :: ERR_EMPTY_RESPONSE”错误? - How to fix “net::ERR_EMPTY_RESPONSE” error from Jquery Ajax Post Request? XHR请求-&gt;获取ERR_EMPTY_RESPONSE - XHR request -> getting ERR_EMPTY_RESPONSE HUE API JavaScript HTTP PUT请求返回“ net :: ERR_EMPTY_RESPONSE” - HUE API JavaScript HTTP PUT Request returning “net::ERR_EMPTY_RESPONSE” Chrome Dev工具不会显示Ajax请求响应 - Chrome Dev tools won't show ajax request response net::ERR_EMPTY_RESPONSE 当 API 请求作为响应返回时 - net::ERR_EMPTY_RESPONSE when API request is made in return of react 在 chrome 中向 python 服务器发送请求时出现 ERR_INVALID_HTTP_RESPONSE - ERR_INVALID_HTTP_RESPONSE when sending request in chrome to python server 使用Chrome开发工具调试发布请求 - Debugging Post Request with Chrome Dev Tools AJAX响应错误:net :: ERR_EMPTY_RESPONSE - AJAX response error: net::ERR_EMPTY_RESPONSE 如果您从 chrome 开发工具控制台发出 http 请求,它会包含您当前所在页面的 cookie 吗? - If you issue an http request from the chrome dev tools console, will it include the cookies of the page you're currently on?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM