繁体   English   中英

将 jQuery 连接到 REST 服务

[英]Connect jQuery to a REST Service


我正在尝试使用一个使用jtable连接到后端服务的 jQuery 示例。( https://www.simplecodestuffs.com/pagination-in-java-web-applications-using-jquery-jtable-plugin/ )。 我已经对其进行了调整,以达到以 JSON 形式生成和使用数据的 REST 服务:

<!DOCTYPE html>
<html>
<head>
<link href="css/metro/blue/jtable.css" rel="stylesheet" type="text/css" />
<link href="css/jquery-ui-1.10.3.custom.css" rel="stylesheet"
    type="text/css" />
<script src="js/jquery-1.8.2.js" type="text/javascript"></script>
<script src="js/jquery-ui-1.10.3.custom.js" type="text/javascript"></script>
<script src="js/jquery.jtable.js" type="text/javascript"></script>

<script type="text/javascript">
    $(document).ready(function() {
        $('#StudentTableContainer').jtable({
            title : 'Students List',
            paging: true, //Enable paging
            pageSize: 3, //Set page size (default: 10)           
            actions: {
                listAction: 'http://localhost:8080/Controller/list',
                createAction:'http://localhost:8080/Controller/create',
                updateAction: 'http://localhost:8080/Controller/update',
                deleteAction: 'http://localhost:8080/Controller/delete'
            },
            fields : {
                id : {
                    title : 'id',
                    sort :true,
                    width : '30%',
                    key : true,
                    list : true,
                    edit : false,
                    create : true
                },
                name : {
                    title : 'Name',
                    width : '30%',
                    edit : true
                },
                department : {
                    title : 'Department',
                    width : '30%',
                    edit : true
                },
                emailId : {
                    title : 'Email',
                    width : '20%',
                    edit : true
                }
            }
        });
        $('#StudentTableContainer').jtable('load');
    });
</script>

</head>
<body>
    <div
        style="width: 80%; margin-right: 10%; margin-left: 10%; text-align: center;">
        <h4>Pagination in Java Web Applications jTable</h4>
        <div id="StudentTableContainer"></div>
    </div>
</body>
</html>

不幸的是,jQuery 代码无法连接到 REST 服务:“与服务器通信时发生错误。” 但是,如果我尝试通过 curl 请求它们,REST 服务将正常工作。 谁能给我一个线索,上面的代码有什么问题?

编辑:我从 Firefox 调试器中捕获了正在发生的事情的痕迹。 使用 POST 请求 'list?jtStartIndex=0&jtPageSize=3' 时似乎有错误。 如何将其配置为使用 GET http://localhost:8080/Controller/list 在此处输入图片说明 谢谢

尝试按照记录的示例使用jtable ajax 设置

ajaxSettings: {
    type: 'GET',
    dataType: 'json'
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM