jQuery中ajax和post處理json的不同

來源:互聯網
上載者:User

標籤:io   os   ar   java   sp   資料   on   問題   cti   

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

 

function haha() {jQuery.post("addComment!comment.action", function aa(data) {  data = eval(data);//POST方法必加,ajax方法自動處理了  alert(data[0].userId);  alert(data[0].userName);},"json");jQuery.ajax({type:"post",url:"addComment!comment.action",dataType:"json",success: function aa(data) {  alert(data[0].userId);  alert(data[0].userName);}});}

 

 

後台:

public String comment() {try{User u = new User("user", "koko");list = new ArrayList<User>();list.add(u);//map.put("id", userId);//JSONObject jb = JSONObject.fromObject(list); // name:"+userName +",//info = jb.toString();System.out.println(list);}catch (Exception e) {e.printStackTrace();}return SUCCESS;}

 

配置:

<package name="ajax" extends="json-default"><action name="addComment" class="org.test.action.CommentAction"><result type="json">   <param name="root">list</param></result></action>。。。。。。

 

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.