1、定义页面click按钮,通过此按钮触发ajax异步取后台数据功能
<!DOCTYPE html>
<html>
<body>
<div id="demo">
<h2>Let AJAX change this text</h2>
<button type="button" onclick="loadDoc()">Change Content</button>
</div>
</body>
</html>
2、定义函数loadDoc来执行ajax与服务器交互的动作:
function loadDoc() {
//定意思XMLHttpRequest对象
var xhttp = new XMLHttpRequest()
//定义返回状态为成功时的返回结果显示
xhttp.onreadystatechange = function() {
//返回值状态为4或者响应码为200是成功
if (this.readyState == 4 &&this.status == 200) {
//给标签div赋值返回结果responseText
document.getElementById("demo").innerHTML = this.responseText
}
}
//开始执行后台取数据
xhttp.open("GET", "ajax_info.txt", true)
//开始发送请求
xhttp.send()
}
用js获取之后,用post或者get传到控制器里面,也就是一个PHP页面,在页面里面进行处理。然后创建一个Model或者不创建也可以,直接连接数据库,把你接受的数据存在数据库里面。简单就是js->controller->Model交互生成js不能获取数字后的字符的主要解决方式如下。使用parseInt()1var str ="4500元"2var num = parseInt(str)3 alert(num)//4500 对,你没有看错,parseInt()方法参数可以有非数字字符串。