设置不自动填充表单 这个应该要把js写在使用这个方法的后面,不然就会readonly
设置不自动显示input下面的提示
<input type="text" autocomplete="off" readonly="readonly" placeholder="请输入消息" id="send_txt"
onkeydown=inputEnterSendMessage()>
用js动态的添加网页标签
// 把自己的信息添加到content
function addMyMessageToContent(data) {
var html = "";
html += "<li><span class='spanright'>" + data + "</span></li>";
$(".chat_content").append(html);
// 下面这句是删除原来的东西
// $(".chat_content").html(html);
}
ajax异步请求信息 ( message是一个json格式的东西)
var message = {"message": send_message, "userId": send_userId};
$.ajax({
url: "connectRobot",
dataType: "json",
data: message,
type: "post",
async: true,//是否异步请求
success: function (data) { //如果发送成功
//下面是上面那个自己写的函数
addRobotMessageToContent(data)
send_txt.val("value", "");//.value="";
}
})
他的后台代码
@ResponseBody //这是在返回时不用json的东西,他会自己给你转成json
@RequestMapping("connectRobot") //目前只能返回list,才能在ajax中得到对象的属性
public ArrayList<RobotMessage> connectRobot(HttpServletResponse response, HttpServletRequest request) throws IOException {
response.setContentType("text/text"); //设置请求以及响应的内容类型以及编码方式
response.setCharacterEncoding("UTF-8");
String message = request.getParameter("message").trim();
String userId = request.getParameter("userId").trim();
if(message.equals("")){
return null;
}
ArrayList<RobotMessage> arrayList = new ArrayList<>();
try {
//RobotMessage是我自己定义的一个类,下面这句的意思就是得到前端JS发来的消息
RobotMessage robotMessage = connectRobot.getResponse(message,userId);
System.out.println(robotMessage.toString());
arrayList.add(robotMessage);
return arrayList;
} catch (Exception e) {
System.out.println("错误 : " + e.getMessage());
}
return null;
}
设置input为空,这个是真的坑
var send_txt = document.getElementById('send_txt');
send_txt.val("value", "");//.value="";