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传递参数springmvc怎么接收
比如ajax写法$("#test2").on("click",function(){ var id =1; var username ="fangxin"; $.post("/mvc/client1/test2",{id:id,username:username,birthday:new Date()}) });
Spring后台接收 @RequestMapping("/test2") @ResponseBody public void test2(User user){ System.out.println(user.getId())
; System.out.println(user.getUsername())
; System.out.println(user.getBirthday()); }
ajax请求为post时,服务器端如何获取参数
这取决于post时的mime类型以及后台语言。比如默认情况下mime类型应该是application/x-www-form-urlencoded,后台语言假定是php,则这样读取:
$_POST["key"]
如果是asp则这样: request.form("key") 或 request("key")
另外,当上传的是文件时,则mime类型通常是multipart/form-data
如果上传的是json对象,则类型为application/json
不同的类型,后台就要用不同的方式去处理。
还没有评论,来说两句吧...