2012-03-23 90 views
0

*在我們的代碼中,來自Ext JS頁面的參數被傳遞到Spring 3控制器,我們擁有業務邏輯。然後在控制器中,使用getWriter.write設置響應對象,並在Ext JS頁面中檢索響應。 問題:在解碼響應時,Firebug在使用Ext.util.JSON.decode時顯示錯誤,因此我們不得不使用Ext.decode來解碼來自服務器的響應。但Ext.decode提供了一個值:object Object。我需要將其轉換爲字符串或格式。 控制器代碼:將響應從服務器(JSON對象)轉換爲extjs中的字符串

import java.io.IOException; 
import javax.servlet.http.HttpServletResponse; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RequestParam; 
import org.springframework.web.bind.annotation.ResponseBody; 
@Controller 
@RequestMapping 
public class SampleController { 

    @RequestMapping(value = "/login.htm",method = RequestMethod.POST) 
    @ResponseBody 
    public void validateCredentials(@RequestParam("user") String user, 
      @RequestParam("password") String password,HttpServletResponse response) { 
     boolean flag = false; 
     String resultString = null; 





     try { 
      response.setContentType("application/json"); 
      response.setHeader("Content-Type", "text/html, charset=utf-8"); 


      if (user.equals(password)) { 


       flag = true; 
       resultString = "{success:true}"; 
      } else { 


       flag = false; 
       resultString = "{success:false}"; 
      } 

      response.getWriter().write(resultString); 

     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 


    } 
} 
Ext JS Login form : 
Ext.onReady(function(){ 


    function submit(button,event){ 


       var uname=Ext.getCmp('user').getValue(); 
      alert("1"+uname); 
       var passWord=Ext.getCmp('password').getValue(); 

      Ext.Ajax.request({ 
        url: 'login.htm', 
        method :'POST', 

        params: { 
         user:uname, 
         password:passWord 
        }, 
        success: function(result,request) { 
         var jresp = Ext.JSON.decode(result.responseText); 
//Ext.JSON.decode stores object Object in jresp. Our requirement is converting jresp to String or boolean 
         console.log('Success'+jresp); 



        }, 
        failure: function(response, request) { 
         var jresp = Ext.JSON.decode(result.responseText); 
         console.log(jresp.error); 
         console.log('server-side failure with status code 8 ' + response.status); 
        } 
       }); 





      } 




    var myform = new Ext.form.FormPanel({ 
      title:'Login form', 
      frame:true, 
      width:400, 
      height: 250, 

      url:'login.htm', 
      method:'POST', 
      renderTo:'div1', 
      items:[ 
        { 
         xtype:'textfield', 
         fieldLabel:'user', 
         inputType:'user', 
         allowBlank:false, 
         minLengthText:'3', 
         id:'user', 
         name:'user' 
        }, 
        { 
         xtype:'textfield', 
         fieldLabel:'password', 
         inputType:'password', 
         allowBlank:false, 
         minLengthText:'3', 
         id:'password', 
         name:'password' 
        } 
        ], 
        buttonAlign:'center', 
        buttons:[ 
          { 
           text:'reset',handler:function(){myform.getForm().reset();} 
          }, 
          { 

           text:'Login', 
           handler: submit 
          } 

          ]  

        }); 






}); 
+0

你能發表你的回覆嗎?如果是JSON,你應該可以使用JSON.Decode – sha 2012-03-23 10:58:56

回答

0

解碼時的響應,螢火顯示了使用 Ext.util.JSON.decode一個錯誤,所以我們只好用Ext.decode代替解碼我們從服務器 響應。

你,因爲你正在使用錯誤的對象ExtJS4其Ext.JSON.decode編碼不Ext.util.JSON.decode,你是因爲ExtJS3爲具有Ext.util.JSON有這種混亂的數據得到錯誤.decode解析JSON字符串,但是,是故障安全的方式將是使用Ext.decode這是簡寫形式,在ExtJS3正確的功能和ExtJS4都

但Ext.decode給出一個值:Object對象。我需要將其轉換成 字符串或格式

因爲你,所以當你解析JSON字符串你會得到這樣的事情

數據= {'成功生成響應像"{success:true}"這是真的':true};

,所以你可以通過使用data.success

讓您的布爾值,所以你的最終代碼看起來像這樣

var jresp = Ext.JSON.decode(result.responseText); 
console.log('Success'+jresp.success); 

一件事你正使用該字符串

生成Spring MVC中響應

resultString =「{success:true}」;

這不是有效的JSON字符串,所以你需要附上對象的關鍵在雙引號像這樣

resultString =「{\」成功\「:真正}」;

0

爲什麼不傳遞一個Model實例包含您在javascript(ExtJs)中需要的所有字段,您可以使用jackson將您的模型轉換爲JSON。

剛剛宣佈在appContext您的轉換器:

和包括傑克遜罐子。

您還可以將成功和消息放入模型中。爲了簡單起見,使用帶有成功和消息字段的抽象ExtJsModel,並使所有模型擴展相同的模型。

0

您可以將這樣的回覆發送給extjs。然後使用extjs解碼器。 這是代碼。

var jresp = Ext.util.JSON.decode(result.responseText); console.log(jresp.success);

「」 「{成功:真正}」 「」

如果你安慰這個樣子。 console.log('Success'+ jresp.success);

java-script總是給object.So不要用這個。

相關問題