使用AJAX返回JSON数据格式的步骤如下:
创建一个XMLHttpRequest对象:var xhr = new XMLHttpRequest();监听服务器响应:xhr.onreadystatechange = function() {if (xhr.readyState === 4 && xhr.status === 200) {// 处理服务器响应var response = JSON.parse(xhr.responseText);// ...}};打开一个请求:xhr.open('GET', 'http://example.com/api/data', true);设置请求头,以指定响应类型为JSON:xhr.setRequestHeader('Content-Type', 'application/json');发送请求:xhr.send();在服务器端,将数据以JSON格式返回给客户端。
注意:以上代码是使用原生JavaScript实现的AJAX请求,如果使用jQuery等类库,可以更简化地实现。

