jQuery中ajax和post處理json的不同

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   ar   java   for   

近日在做門戶的使用者評論時,好長時間沒有用jquery了正好用一下,沒想到偷工用了post方法去處理ajax回調的json資料,死活取不到,後台就是有json返回了。不料這麼小小一個問題掛了我好幾個小時,後來我ajax方法處理,居然OK,一比較發現原來post方法回調json必須eval一下,而ajax方法做了預設處理了。 望各位小心。。。 

[javascript] view plaincopyprint?
  1. function haha() {  
  2.     jQuery.post("addComment!comment.action",  
  3.      function aa(data) {  
  4.           data = eval(data);//POST方法必加,ajax方法自動處理了  
  5.           alert(data[0].userId);  
  6.           alert(data[0].userName);  
  7.     },  
  8.     "json"  
  9.     );  
  10.       
  11.     jQuery.ajax({  
  12.         type:"post",  
  13.         url:"addComment!comment.action",  
  14.         dataType:"json",  
  15.         success: function aa(data) {  
  16.               alert(data[0].userId);  
  17.               alert(data[0].userName);  
  18.         }  
  19.     });  
  20. }  

 

後台:

[java] view plaincopyprint?
  1.     public String comment() {  
  2.         try{  
  3.         User u = new User("user", "koko");  
  4.         list = new ArrayList<User>();  
  5.         list.add(u);  
  6.         //map.put("id", userId);  
  7. //      JSONObject jb = JSONObject.fromObject(list); // name:"+userName +",  
  8. //      info = jb.toString();  
  9.         System.out.println(list);  
  10.         }  
  11.         catch (Exception e) {  
  12.             e.printStackTrace();  
  13.         }  
  14.         return SUCCESS;  
  15.     }  

 配置:

[html] view plaincopyprint?
  1.     <package name="ajax" extends="json-default">  
  2.         <action name="addComment" class="org.test.action.CommentAction">  
  3.         <result type="json">  
  4.            <param name="root">list</param>  
  5.         </result>  
  6.         </action>  
  7. 。。。。。。  

jQuery中ajax和post處理json的不同

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.