亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如果存儲為對象,如何檢索數據?

如果存儲為對象,如何檢索數據?

LEATH 2021-05-07 09:21:49
我正在使用ajax將數據傳遞到servlet。據我所知,標準方法通常是這樣的:$.ajax({               url: 'page.jsp',               type: 'POST',               data: {                     id:value,                     name:value2               },               success: function (data) {                   alert("Successfully initiated email to queue");               },               error: function (request, error) {                   alert("Request: " + JSON.stringify(error));               }           });然后在jsp頁面中,將這樣檢索數據:String id = request.getParameter("id");String name = request.getParameter("name");毫無疑問,這將起作用?,F在,如果我想將數據存儲為對象怎么辦。在我的JavaScript中是這樣的: var data;     if(condition){       data={                'recipient': recipient,                'subject': subject,                'content': content,                'id':"<%=id%>",                'hash':"<%=hash%>",                'multiemail':"no"            }    }else{          data= {                'recipient': recipient,                'subject': subject,                'content': content,                'arrayList':<%=array%>,                'multiemail':"yes"            }    }  $.ajax({               url: 'page.jsp',               type: 'POST',               data: {                     info:data               },               success: function (data) {                   alert("Successfully initiated email to queue");               },               error: function (request, error) {                   alert("Request: " + JSON.stringify(error));               }           });然后,我使用相同的方式: String recipient = request.getParameter("recipient");這將返回空值。如何準確地從對象中檢索所需的數據值?
查看完整描述

2 回答

?
喵喔喔

TA貢獻1735條經驗 獲得超5個贊

當您使用以下代碼行時,String recipient = request.getParameter("recipient"); 它將在


           $.ajax({

           url: 'page.jsp',

           type: 'POST',

           data: {


                 info:data


           }

但是,幸運的是,沒有收件人,您的ajax中只有信息密鑰。因此,您可以使用getParameter(“ info”)來獲取數據?,F在您有了數據。


請參考以下代碼


$.ajax({

           url: 'page.jsp',

           type: 'POST',

           data: data

我想現在你可以使用 String recipient = request.getParameter("recipient");


查看完整回答
反對 回復 2021-05-13
?
嚕嚕噠

TA貢獻1784條經驗 獲得超7個贊

request.getParameter("recipient");會在您的數據中尋找收件人密鑰。但是您的密碼info不是recipient(這是信息的一部分)。要訪問收件人,您必須首先request.getParameter("info")使用任何JSON解析庫解析收到的JSON(),然后從已解析的JSON對象訪問收件人。


在您的ajax中,以json格式傳遞數據


$ .ajax({


           url: 'page.jsp',

           type: 'POST',

           dataType: 'JSON',

           data: {

                 info:data

           },


           success: function (data) {

               alert("Successfully initiated email to queue");

           },

           error: function (request, error) {

               alert("Request: " + JSON.stringify(error));

           }

       });

在您的servlet端,像這樣解析json:


JsonParser parser = new JsonParser();


String json = request.getParameter("info");


JsonElement jsonTree = parser.parse(json);

String recipientjsonTree.get("recipient");

JsonParser 是GSON庫的一部分。


查看完整回答
反對 回復 2021-05-13
  • 2 回答
  • 0 關注
  • 186 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號