0
我做了一个有输入的HTML页面。我通过javascript获取了值,创建了一个JSON,并且我想通过ajax发送它。我也有一个JSP应用程序,它在java中运行一个方法来接收这个JSON并读取它,以便它可以存储在数据库中。问题是我不知道如何在我的jsp应用程序中接收来自ajax的调用,并将它发送给我的java方法。有人可以帮我弄这个吗?接收POST请求从javascript发送JSON在jsp中
的Javascript:
alert("I am about to POST this:\n\n" + dat);
$.ajax({
url: '/path/to/file',
type: 'POST',
dataType: 'JSON',
data: dat,
})
.done(function() {
console.log("success");
})
.fail(function() {
console.log("error");
})
.always(function() {
console.log("complete");
});
`
JSP:
'<%@ page language="java" import="connection.JsonHandler" %>
<%
String json = request.getParameter("dat");;
JsonHandler gson = new JsonHandler();
gson.ReadJson(json);
%>
的Java:
package connection;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.google.gson.GsonBuilder;
import entidades.User;
import java.lang.reflect.Type;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
public class JsonHandler {
public Gson CreateJson(String values) {
Gson gson = new GsonBuilder().create();
gson.toJson("Hello", System.out);
gson.toJson(123, System.out);
return gson;
}
public void ReadJson(String json){
Gson gson = new Gson();
Type type = User.class;
gson.fromJson(json,type);
}
}
你试过了什么代码?您没有发布任何代码 – jhhoff02
对不起,我真的很陌生,所以我没有做太多 – Irettam