[英]convert the string in servelt to json data and pass it to javascript : ajax
我需要以 ajax 方式提交表單。 當表單提交時,將 servlet 中的字符串轉換為 json 數據並將其傳遞給 javascript 函數,並在每個輸入字段下方的錯誤 div 標記附近分別顯示這些錯誤。
if(firstparamtr-condition)
{
String firsterror = "Error first";
response.setContentType("application/json");
response.setHeader("Cache-Control", "no-cache");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(firsterror);
}
if(secondparamtr-condition)
{
String seconderror = "Error second";
response.setContentType("application/json");
response.setHeader("Cache-Control", "no-cache");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(seconderror);
}
javascript代碼
posting.done(function( responseText ) {
alert(responseText);
//HERE I need to separate the responseText and show that errors in the div tags named 'firstEr' and 'secondEr'
});
為了將 String 轉換為 JSON,我使用GSON轉換器,示例如下:
String string = "example"; // The String which you wish to convert JsonObject convertedObject = new Gson().fromJson(string, JsonObject.class);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.