ajax请求,html调用js

1:html中调用js中的函数,js使用ajax请求向后台请求,返回数据。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <script>
    var url;   //定义变量url
    function link() {
        //你的功能
   $.ajax({     //ajax请求
type: "get", //请求方式
data: { //向后台传输的数据,字段
"userid":userId
},
    url: "<%=path%>/download.do",   //请求接口,url
async: false, //非异步请求
success: function (data) { // 回调函数,成功之后,后台返回data
url = data; //data赋值给url
}
});
window.location.href = url;   //使用url
}
</script>

</head>

<body>
<a href="javascript:void(0)" onclick="link()">客户端下载</a> //调用js函数

</body> </html>

猜你喜欢

转载自www.cnblogs.com/liyafei/p/9293370.html