Flask中服务器端怎样接受ajax发送的json
在stack overflow找到了一个临时解决方法,修改如下:
data:{
'n1': $('input[name=n1]').val(),
'n2': $('input[name=n2]').val(),
'n3': $('input[name=n3]').val(),
},
修改为:
data:JSON.stringify({
'n1': $('input[name=n1]').val(),
'n2': $('input[name=n2]').val(),
'n3': $('input[name=n3]').val(),
}),
ajaxpost带参数请求后台怎么接收参数
把要发送的json作为字符串传入body即可
public static String sendHttpPost(String url, String body) throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", "application/json");
httpPost.setEntity(new StringEntity(body));
CloseableHttpResponse response = httpClient.execute(httpPost);
System.out.println(response.getStatusLine().getStatusCode() + "\n");
HttpEntity entity = response.getEntity();
String responseContent = EntityUtils.toString(entity, "UTF-8");
System.out.println(responseContent);
response.close(); httpClient.close(); return responseContent; }
ajax获取数据库的数据
function showLogs() { $("#table2").empty(); //这是清空原来的数据 $.ajax({ type:"post", url:"<%=request.getContextPath()%>/logs/queryLogsInfo.action", dataType:"json", success:function(data) { for ( var i = 0; i < data.length; i++) { $("#table2").append("<tr style='line-height:25px;'>" +"<td width='5%'>"+data[i].id+"</td>" +"<td width='5%'>"+data[i].receive+"</td>" +"<td width='8%'>"+data[i].data+"</td>" +"<td width='10%'>"+data[i].dataLength+"</td>" +"</tr>"); } } }) } var t = setInterval("showLogs()", 1000); //隔1秒就查询一次数据
还没有评论,来说两句吧...