简体   繁体   English

通过来自多个客户端的 Ajax PUT 请求发送数据

[英]Sending data via Ajax PUT-request from multiple clients

I've wrote a small application which is fetching data from a server via ajax GET and loads this data into an html table.我编写了一个小应用程序,它通过 ajax GET 从服务器获取数据,并将这些数据加载到 html 表中。 Furthermore the user can add data into the tablerows by filling out a form or remove data via checkbox.此外,用户可以通过填写表格或通过复选框删除数据来将数据添加到表格中。 Then all data from the table gets send to the server via ajax PUT.然后表中的所有数据都通过 ajax PUT 发送到服务器。 Before sending there will be a get-request again to get the latest data from the server.在发送之前,将有一个 get-request 再次从服务器获取最新数据。 So far so good.到目前为止,一切都很好。

Now I am facing the issue that when the website gets accessed from multipe clients the second get-request works not proper.现在我面临的问题是,当网站从多个客户端访问时,第二个 get-request 工作不正常。 The data is not add when sending, it's getting overwritten by the entries/input from the other user.发送时未添加数据,它被其他用户的条目/输入覆盖。

Does anyone ahs an idea how to solve this issue?有谁知道如何解决这个问题? Thank you in advance!先感谢您!

GET-request GET请求

  function getMessage() {
    // Send HTTP Request
      $.ajax({
          type: 'GET',
          url: endpointUrlGet,
          contentType : 'application/json',
          success: function(data) {
              // Loading data from get request into table
                  var filter_data = '';
                  $.each(data, function(index, value) {
                      filter_data += '<tr>';
                      filter_data += '<td><input type="checkbox" /></td>';
                      filter_data += '<td>' + value.nummer+ '</td>';
                      filter_data += '<td>' + value.start+ '</td>';
                      filter_data += '<td>' + value.time+ '</td>';
                      filter_data += '<td>' + value.type+ '</td>';
                      filter_data += '<td>' + value.start_time+ '</td>';
                      filter_data += '<td>' + value.end_time+ '</td>';
                      filter_data += '</tr>';
                  });
                  $("#entries").append(filter_data );
          },
          error: function(d) {
              alert("Fehler beim Laden.")
          }
      })
  }

PUT-request PUT-请求

function sendMessage(payload) {
      // Send HTTP Request
      $.ajax({
        type: 'PUT',
        url: endpointUrlPut,
        contentType : 'application/json',   
        data : payload,
        statusCode: {
            200: function() {
                location.reload();
                alert("Success.");
            },
            400: function() {
                alert( "Fehler." );
                console.log("Bad request.");
              },
            403: function() {
                alert( "Fehler." );
                console.log("Forbidden.");
                },
            404: function() {
                alert( "Fehler." );
                console.log("Page not found.");
              },
            500: function() {
                alert( "Fehler." );
                console.log("Internal Server error.");
              },
            502: function() {
                alert( "Fehler." );
                console.log("Bad Gateway.");
                },
            503: function() {
                alert( "Fehler." );
                console.log("Service Unavailable.");
                }
          },
          error: function(d) {
              alert("Fehler beim Senden.");
          }
     })       
  }

Method-calls:方法调用:

$('#submit_btn').click(function() {

        getMessage();
        sleep(100);


        $("#entries input[type='checkbox']:checked").closest("tr").remove();    
        var table= $('#entries tr:has(td)').map(function(i, v) {
            var $td =  $('td', this);
                return {
                         nummer: $td.eq(1).text(),
                         start: $td.eq(2).text(),
                         time: $td.eq(3).text(),
                         type: $td.eq(4).text(),
                         start_time: $td.eq(5).text(),
                         end_time: $td.eq(6).text()
                       }
        }).get();

        // Parses the tabledata into required JSON-format
        var data = JSON.stringify({
            "env" : "dev",
            "allData" : table
        })


        // Sending the filters via PUT
        sendMessage(data);

      });
     

I think its more about the implementation rather than your Ajax request.我认为它更多的是关于实现而不是你的 Ajax 请求。 You need to synchronize the access to the resource.您需要同步对资源的访问。

What i meant by this comment is, are multiple clients are allowed to access the resource simultaneously?我的意思是,是否允许多个客户端同时访问资源? If allowed on what basis the updates are made?如果允许,更新的依据是什么? What i feel is two clients should not be allowed to access the same resource simultaneously, other wise it would lead to data inconsistency as in your case.我觉得不应允许两个客户端同时访问同一资源,否则会导致数据不一致,就像您的情况一样。 You need to apply the right synchronization technique to achieve the desired output.您需要应用正确的同步技术来实现所需的 output。 Hope this is clear!希望这很清楚!

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

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