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