请问html的js调用webapi接口?

JavaScript029

请问html的js调用webapi接口?,第1张

引用jquery,有很方便的GET调用方法:

<!DOCTYPE html>

<html>

<head>

    <meta http-equiv="content-type" content="text/html charset=UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- src值为文件位置路径 -->

    <script type="text/javascript" charset="UTF-8" src="javascript/jquery-1.12.1.js"></script>

    <title>测试案例</title>

    <!-- 语法:jQuery.getJSON(url,data,success(data,status,xhr)) -->

    <script type="text/javascript" charset="UTF-8">

        function getToken(){

            $.getJSON("http://localhost/kdapi/api/access_token", {"id":111,"secret":2352532}, function(result){

                alert(result.access_token)

            })

        }

    </script>

</head>

<body>

    <button onclick="getToken()" style="width: 120px height: 60px">获取Token</button>

</body>

</html>

js调用webapi如何传递日期类型参数

先把jsp里面的日期格式化成字符串,然后传给js当作参数传到Date就可以了。

1、格式化jsp的时间:

<%@ page contentType="text/htmlcharset=gb2312"%>

<%@ page import="java.util.*"%>

<%@ page import="java.text.*"%>

<html>

<body>

现在的时间:

<%SimpleDateFormat s = new SimpleDateFormat("yyyyMMddHHmmss")

Date date = new Date()

%>

<%=s.format(date)%>

</body>

</html>

2、传值给js

var dt1="<%=s.format(date)%>"

var oDate1 = new Date(dt1)

这样就可以把字符串的日期转换成js的date类型了。