前端如何接收后端返回的字符串
如果你在前端使用的是JavaScript,你可以使用AJAX(Asynchronous JavaScript and XML)来从后端接收String。以下是一个简单的例子:
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var responseString = this.responseText; // 接收到的字符串
// 在这里处理接收到的字符串
}
};
xhttp.open('GET', 'url/to/backend', true);
xhttp.send();
在上面的代码中,通过XMLHttpRequest对象发起了一个GET请求,将从后端获取字符串。当得到响应时,onreadystatechange函数会被调用,可以通过responseText属性获取返回的字符串。
原文地址: https://www.cveoy.top/t/topic/l4fB 著作权归作者所有。请勿转载和采集!