当前位置:   article > 正文

extjs4 表单提交 和 Ajax请求 示例_"return \"{success : false ,msg :"

"return \"{success : false ,msg :"

表单的提交form.submit和Ajax请求里的success和failure回调函数有点区别,

表单提交后要求后台返回的必须是json字符串,且必须包含success:true 或 success:false,如果不包含这个直接返回一个json字符串就会执行failure函数,这个郁闷我一下午……

Ajax请求则不同,只要后台返回一个字符串就能成功接收到


表单提交(这里我就直接写handler了)

  1. handler : function() {
  2. var form = me.down('form').getForm();
  3. if (form.isValid()) {
  4. form.submit({
  5. url : 'http://localhost:8080/b_springmvc_extjs/formSubmit.do',
  6. method : 'POST',
  7. waitTitle : "提示",
  8. waitMsg : '正在提交数据,请稍后 ……',
  9. success : function(form, action) {
  10. Ext.Msg.alert('success', 'bbb ' + form
  11. + action.result.msg);
  12. this.close();
  13. },
  14. failure : function(form, action) {
  15. Ext.Msg.alert('failure', 'bbb' + form
  16. + action.result.msg);
  17. this.close();
  18. }
  19. });
  20. }
  21. }

后台

  1. @RequestMapping(value = "/formSubmit.do", method = RequestMethod.POST)
  2. @ResponseBody
  3. public String formSubmit(@ModelAttribute(value = "user") User user) {
  4. if (user != null || !equals("")) {
  5. System.out.println(user.getId());
  6. System.out.println(user.getFirstname());
  7. System.out.println(user.getLastname());
  8. try {
  9. Thread.sleep(1000 * 2);
  10. } catch (InterruptedException e) {
  11. e.printStackTrace();
  12. }
  13. return "{success:true,msg:'success submit --- yang'}";
  14. }
  15. return "{success:false,msg:'failure submit --- xuan'}";
  16. }




然后是Ajax请求,这里我是监听一个combo的focus事件(mvc模式)

  1. 'frameNorth combo[id=changeSkin]' : {
  2. focus : this.comboFocus
  3. }
  1. comboFocus : function(combo, The, eOpts) {
  2. Ext.Ajax.request({ //ajax request test
  3. url : 'http://localhost:8080/b_springmvc_extjs/ajaxRequest.do',
  4. /* headers: {
  5. 'userHeader': 'userMsg'
  6. },*/
  7. params : {
  8. name : 'yangxuan'
  9. },
  10. method : 'POST',
  11. success : function(response, options) {
  12. Ext.MessageBox.alert('成功', '服务端返回数据 : '+ response.responseText);
  13. },
  14. failure : function(response, options) {
  15. Ext.MessageBox.alert('失败', '错误编号:' + response.status);
  16. }
  17. });
  18. },

后台

  1. @RequestMapping(value = "ajaxRequest.do", method = RequestMethod.POST)
  2. @ResponseBody
  3. public String ajaxRequest(@RequestParam(value = "name") String name) {
  4. if (name != null || !name.equals("")) {
  5. System.out.println("name : " + name);
  6. return "success ajax request";
  7. }
  8. return "fail ajax request";
  9. }




声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/322169
推荐阅读
相关标签
  

闽ICP备14008679号