Ext.Ajax獲取返回值(引數)
阿新 • • 發佈:2019-02-02
本文轉載自:http://blog.csdn.net/dingherry/article/details/6798816
[javascript] view plain copy- Ext.Ajax.request({
- url:'http://localhost:8080/myapp/ExtHandler,
- jsonData:Ext.util.JSON.encode(info),
- params:{action:'up'},
-
success: function
- var respText = Ext.util.JSON.decode(resp.responseText);
- Ext.Msg.alert('提示', respText.info);
- },
- failure: function(resp,opts) {
-
var respText = Ext.util.JSON.decode(resp.responseText);
- Ext.Msg.alert('錯誤', respText.error);
- }
- });
程式碼中的url引數指定的是一個Java Servlet,通過jsonData引數提交JSON格式的資料到Servlet處理,你也可以提交其它引數,在params引數中定義;然後根據伺服器的處理結果Ext.Ajax呼叫相應成功或失敗的回撥函式進行處理;
在Servlet中如何得到jsonData引數中定義的資料呢?看以下程式碼:
-
StringBuffer json = new StringBuffer();
- String line = null;
- try {
- BufferedReader reader = req.getReader();
- while ((line = reader.readLine()) != null) {
- //讀取jsonData中定義的資料
- json.append(line);
- }
- } catch (Exception e) {
- }
服務端處理資料成功,設定返回資訊:
[javascript] view plain copy- rsp.setContentType("text/json; charset=utf-8");
- rsp.getWriter().write(
- "{success:false,error:'更新資訊失敗,原因為:" + err + "'}");
- rsp.setStatus(HttpServletResponse.SC_EXPECTATION_FAILED); //設定失敗標識
- //failure回撥函式將呼叫執行,輸出respText.error資訊