简体   繁体   English

使用AJAX消费Web服务

[英]Consume Web Service with AJAX

I am trying to invoke a live web service in Json Format - 我正在尝试以Json格式调用实时Web服务-

{"GetAllBookingsResult":[{"BookingID":47,"CaseID":0,"Id":0,"ava":0,"inspectionDate":"Wednesday, December 30, 2015","inspectionRemarks":"okokokoko","inspectionTime":"06: 00 PM - 07: 30 PM","selectedDate":null,"value":null},{"BookingID":48,"CaseID":0,"Id":0,"ava":0,"inspectionDate":"Wednesday, December 30, 2015","inspectionRemarks":"okijijoubibiuyv8","inspectionTime":"04: 30 PM - 05: 30 PM","selectedDate":null,"value":null},{"BookingID":50,"CaseID":0,"Id":0,"ava":0,"inspectionDate":"Wednesday, December 30, 2015","inspectionRemarks":"joy2","inspectionTime":"04: 30 PM - 05: 30 PM","selectedDate":null,"value":null},{"BookingID":51,"CaseID":0,"Id":0,"ava":0,"inspectionDate":"update","inspectionRemarks":"update","inspectionTime":"update","selectedDate":null,"value":null},{"BookingID":53,"CaseID":0,"Id":0,"ava":0,"inspectionDate":"Wednesday, December 30, 2015","inspectionRemarks":"kjbkhbj","inspectionTime":"12: 30 PM - 01: 30 PM","selectedDate":null,"value":null},{"BookingID":54,"CaseID":0,"Id":0,"ava":0,"inspectionDate":"Wednesday, December 30, 2015","inspectionRemarks":"okokmokm","inspectionTime":"02: 30 PM - 03: 30 PM","selectedDate":null,"value":null},{"BookingID":55,"CaseID":0,"Id":0,"ava":0,"inspectionDate":"Wednesday, December 30, 2015","inspectionRemarks":"JESUS MAN ","inspectionTime":"02: 30 PM - 03: 30 PM","selectedDate":null,"value":null},{"BookingID":58,"CaseID":0,"Id":0,"ava":0,"inspectionDate":"Wednesday, December 30, 2015","inspectionRemarks":"new update","inspectionTime":"12: 30 PM - 01: 30 PM","selectedDate":null,"value":null},{"BookingID":59,"CaseID":0,"Id":0,"ava":0,"inspectionDate":"Thursday, December 24, 2015","inspectionRemarks":"heheheh","inspectionTime":"02: 30 PM - 03: 30 PM","selectedDate":null,"value":null},{"BookingID":60,"CaseID":0,"Id":0,"ava":0,"inspectionDate":"Tuesday, December 29, 2015","inspectionRemarks":"tESTINGLDFNGLSDNGFLJKDFNG","inspectionTime":"12: 30 PM - 01: 30 PM","selectedDate":null,"value":null}]}

This is my AJAX call in my Javascript 这是我的Java语言中的AJAX调用

<script type="text/javascript">
    $(document).on(function () {
        var GetAllBookings = function () {
            $.ajax({
                type: "GET",
                url: 'http://localhost:41014/WsRepCatalog.svc',
                contentType: "json",
                dataType: "json",
                success: function (data) {
                    $('#tbBookings').show();
                    var response = data.GetAllBookingsResult;
                    $.each(reponse, function (key, value) {
                        //stringify
                        var jsonData = JSON.stringify(value);
                        //Parse JSON
                        var objData = $.parseJSON(jsonData);
                        var BookingID = objData.BookingID;
                        var inspectionDate = objData.inspectionDate;
                        var inspectionTime = objData.inspectionTime;
                        var inspectionRemarks = objData.inspectionRemarks;
                        $('<tr><td>' + BookingID + '</td><td>' + inspectionDate + '</td><td>'
                         + inspectionTime + '</td><td>' + inspectionRemarks +
                         '</td></tr>').appendTo('#tbBookings');
                    });
                },
                error: function (xhr) {
                    alert(xhr.responseText);
                }
            });
        }
    });
</script>

This is where i want to display the data in a table format. 这是我要以表格格式显示数据的地方。

<form onload="GetAllBookings()">
    <p>List of Bookings</p>
    <table border="1" id="tbBookings">
    </table>
</form>

But I can't seem to get it as no data appear. 但是我似乎无法获得它,因为没有数据出现。 Much help needed, thank you! 需要大量帮助,谢谢!

 <form onload="GetAllBookings()"> 

Here you are trying to call GetAllBookings when the load event fires on the form. 在这里,您尝试在load事件在窗体上触发时调用GetAllBookings

Your first problem is that forms do not load content from a URL so there is no load event on them. 您的第一个问题是表单不会从URL加载内容,因此在它们上没有加载事件

Your second problem is that when you defined GetAllBookings you did so here: 您的第二个问题是,在定义GetAllBookings您在这里进行了以下操作:

 $(document).on(function () { var GetAllBookings = function () { 

… inside another function, so it is not a global. …在另一个函数中,因此它不是全局函数。

You're also passing a function as the first argument to on but it makes no sense to so. 您还将函数作为第一个参数传递给on但是这样做没有任何意义。

  1. Get rid of the intrinsic event attribute 摆脱内在事件属性
  2. Pass the function you want to call to the ready event handler 将您要调用的函数传递给ready事件处理程序

Such: 这样:

$(function () {
    $.ajax({
        type: "GET",
        url: 'http://localhost:41014/

And so on. 等等。

Note you are using an absolute URL so it is possible you are going to run cross origin issues. 请注意,您使用的是绝对URL,因此可能会遇到跨源问题。 Look at the Console in your developer tools. 查看开发人员工具中的控制台。 Read the error messages (if there are any). 阅读错误消息(如果有)。 Search Google to find out what they mean. 搜索Google以了解它们的含义。

The given function will get the data from service in json format and in datatable_creationCallBack function you can iterate the response and fetch into HTML table 给定的函数将以json格式从服务中获取数据,而在datatable_creationCallBack函数中,您可以迭代响应并提取到HTML表中

     function initial_table_creation(){
            var objectForPost = null;
            var contentType = 'application/json; charset=utf-8';
            var servletPath = 'http://localhost:41014/WsRepCatalog.svc';
            var method = "GET";

                jQAjaxCallForAccountJSON(servletPath, method, contentType,
                        objectForPost, 'datatable_creationCallBack');
        }
        function datatable_creationCallBack(resultobject){
            var table_head=null,table_body=null;    
            var jndx=1;
            for(var i=0;i<resultobject.appstatus.length;i++){
                table_head="<thead><tr><th>S.No</th><th>Title</th><th>Message</th><th>BranchName</th><th>Exported Datetime</th></tr></thead>";
                table_body=table_body+"<tr><td>"+jndx+"</td><td>"+resultobject.appstatus[i].type+"</td><td>"+resultobject.appstatus[i].message+"</td>" +
                        "<td>"+resultobject.appstatus[i].branchName+"</td><td>"+resultobject.appstatus[i].exportedTime+"</td></tr>";
                jndx++;
                }
            $("#appdatas").html(table_head+"<tbody>"+table_body+"</tbody>");
        }

function jQAjaxCallForJSON(servletPath, method, contentType, objectForPost,
            callBack) {
        var dataInJSON = '';
        if (objectForPost == null) {
            objectForPost = {};
        }

        if ('DELETE' != method) {
            dataInJSON = JSON.stringify(objectForPost);
            $.ajax({
                async : true,
                url : servletPath,
                type : method,
                crossDomain : true,
                beforeSend : function(jqXHR) {
                    jqXHR.setRequestHeader("Content-type", contentType);

                },
                data : dataInJSON,
                dataType : 'json',
                processData : false,
                statusCode : {
                    401 : function() {

                        window.location = path + '/index.jsp';
                    }
                },
                success : function(resultObject) {
                    var callBackfunction = window[callBack];
                    callBackfunction(resultObject);
                },
                error : function(xhr, ajaxOptions, thrownError) {
                    /*console.info('server error');*/
                }
            });
        }
    }

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

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