var xhr = new XMLHttpRequest();\n\nxhr.open('POST', 'handler.php', true);\n\nxhr.onload = function () {\n if (xhr.status !== 200) {\n console.log('An error occurred!');\n }\n};\n\nxhr.send(formData); 这段代码使用 XMLHttpRequest 对象来向服务器发送一个 POST 请求,并将请求结果返回到 xhr.onload 函数中进行处理。首先,它创建了一个 XMLHttpRequest 对象并设置请求的方法为 POST,请求的 URL 为 'handler.php',以及设置请求为异步请求。然后,在 xhr.onload 函数中,它检查服务器返回的状态码,如果不等于 200,则打印出 'An error occurred!'。最后,它发送请求并将 formData 作为请求的数据发送到服务器。

JavaScript XMLHttpRequest POST 请求示例

原文地址: https://www.cveoy.top/t/topic/pp5H 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录