Ajax-我提交后无法显示帖子

时间:2019-07-26 11:24:04

标签: javascript ajax asynchronous post get

提交帖子后,我无法显示它们。我不知道为什么不工作。当我刷新页面时, private void DrawPolygon(List<LatLng> array) { int length = array.size(); if(length == 0) { // Do whatever you like then get out. Do not run the following. return; } PolygonOptions poly = new PolygonOptions(); for (int i = 0; i < length; i++) { LatLng latLong = new LatLng(array.get(i).latitude, array.get(i).longitude); poly.add(latLong); } Polygon polygon = mMap.addPolygon(poly); 函数正在运行,一切正常。但是提交帖子后,我收不到。我在后端使用JSON伪造服务器。

getPosts()

这是请求:

// On load
document.addEventListener('DOMContentLoaded', function () {
  getPosts();
});

// Sumbit post
document.querySelector('#wrapper-form').addEventListener('click', submitPost);

// Function Helpers
function submitPost(e) {
 if(e.target.dataset.role === 'submit-post') {
   http.submitPost();
   getPosts();
}

 e.preventDefault();
}

// Get posts
function getPosts() {
 http.getPosts()
   .then(response => {
     ui.populateList(response);
  })
   .catch(err => console.log(err));
 }

1 个答案:

答案 0 :(得分:0)

我想通了:

http.submitPost()
 .then(response => {
   getPosts();
});

显然,我的新手程序员技能不知道您不能同时有2个请求。因此,我需要先提交,然后再提交,然后才能获得帖子。

首先我做了:

function submitPost(e) {
 if(e.target.dataset.role === 'submit-post') {
   // I did 2 requests as the same time. That was the problem
   http.submitPost();
   getPosts();
}

 e.preventDefault();
}