2012-08-22 61 views
0

我試圖使用Ext direct的Remoting API來調用遠程方法。但遇到以下URL時出現錯誤:「http://10.112.202.164:8080/TestProject/app.html」分機直接與sencha touch 2

錯誤:無法通過XHR同步加載:'\ TestProject \ direct \ undefined。 js'請 驗證文件是否存在。
XHR狀態碼:404 這個GET請求是在POST請求後發送的,POST請求很好的獲取了所需的響應,但之後這個GET請求被觸發了,不知道爲什麼?

我的代碼如下:

app.html:

<!DOCTYPE html> 

<!-- Auto Generated with Sencha Architect --> 
<!-- Modifications to this file will be overwritten. --> 
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>MyExtDirct</title> 
    <link rel="stylesheet" type="text/css" href="lib/resources/css/sencha-touch.css"/> 
    <script type="text/javascript" src="lib/sencha-touch-all-debug.js"></script> 
    <script type="text/javascript"> 
     Ext.ns("Ext.app.REMOTING_API"); 
     Ext.app.REMOTING_API =   {"descriptor":"Ext.app.REMOTING_API","url":"http://10.112.202.164:8080/TestProject/AjaxHandler","type":"remoting","actions":{"TestAction":[{"name":"getCountry","len":0,"formHandler":false}]}}; 
     Ext.Direct.addProvider(Ext.app.REMOTING_API); 
    </script> 
    <script type="text/javascript" src="app.js"></script> 
    <script type="text/javascript"> 
     if (!Ext.browser.is.WebKit) { 
      alert("The current browser is unsupported.\n\nSupported browsers:\n" + 
       "Google Chrome\n" + 
       "Apple Safari\n" + 
       "Mobile Safari (iOS)\n" + 
       "Android Browser\n" + 
       "BlackBerry Browser" 
      ); 
     } 
    </script> 
</head> 
<body></body> 
</html> 

app.js:

Ext.require([ 
    'Ext.direct.*' 
]); 

Ext.application({ 
    stores: [ 
     'MyDirectStore' 
    ], 
    views: [ 
     'MyFormPanel' 
    ], 
    name: 'MyApp', 

    launch: function() { 


     Ext.create('MyApp.view.MyFormPanel', {fullscreen: true}); 

    } 

}); 

MyDirectStore.js:

Ext.define('MyApp.store.MyDirectStore', { 
    extend: 'Ext.data.Store', 

    config: { 
     autoLoad: true, 
     storeId: 'MyDirectStore', 
     proxy: { 
      type: 'direct', 
      directFn: TestAction.getCountry, 
      reader: { 
       type: 'json', 
       record: 'countryName' 
      } 
     }, 
     fields: [ 
      { 
       name: 'countryName' 
      } 
     ], 
     listeners: [ 
      { 
       fn: 'onStoreLoad', 
       event: 'load' 
      } 
     ] 
    }, 

    onStoreLoad: function(store, records, successful, operation, eOpts) { 
     Ext.Msg.alert("Information", "Loaded " + records.length + " records"); 
    } 

}); 

MyFormPanel.js :

Ext.define('MyApp.view.MyFormPanel', { 
    extend: 'Ext.form.Panel', 

    config: { 
     items: [ 
      { 
       xtype: 'textfield', 
       itemId: 'mytextfield', 
       label: 'Country', 
       store: 'MyDirectStore' 
      } 
     ], 
     listeners: [ 
      { 
       fn: 'onMytextfieldFocus', 
       event: 'focus', 
       delegate: '#mytextfield' 
      } 
     ] 
    }, 

    onMytextfieldFocus: function(textfield, e, options) { 
     var store = Ext.getStore('MyDirectStore'); 
     store.load(); 
     TestAction.getCountry(function(result, event) { 
      var transaction = event.getTransaction(), 
      content; 

     if (event.getStatus()) { 
      content = Ext.String.format('<b>Successful call to {0}.{1} with response:</b><pre>{2}</pre>', 
       transaction.getAction(), transaction.getMethod(), Ext.encode(result)); 
     } else { 
      content = Ext.String.format('<b>Call to {0}.{1} failed with message:</b><pre>{2}</pre>', 
       transaction.getAction(), transaction.getMethod(), event.getMessage()); 
     } 


      //updateMain(content); 
      //field.reset(); 
     }); 
    } 

}); 

TestAction.java:

public class TestAction { 
    public String getCountry()throws JSONException{ 
     String country = "India"; 
     JSONObject object = new JSONObject(); 
     object.put("countryName", country); 
     object.put("success", true); 
     String result = object.toString(); 
     return result; 
    } 


} 

AjaxHandler.java:

public class AjaxHandler extends HttpServlet { 
    private static final long serialVersionUID = 1L; 
    /** 
    * @see HttpServlet#HttpServlet() 
    */ 
    public AjaxHandler() { 
     super(); 
     // TODO Auto-generated constructor stub 
     // Object o= SecurityContextHolder.getContext().getAuthentication(); 


    } 

    /** 
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 
    */ 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     // TODO Auto-generated method stub 
     handleAjaxRequest(request, response); 
    } 

    /** 
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 
    */ 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     // TODO Auto-generated method stub 
     handleAjaxRequest(request, response); 


    } 

    private void handleAjaxRequest(HttpServletRequest request, HttpServletResponse response)throws IOException{ 


     String className = ""; 
     String methodName = ""; 
     StringBuffer jb = new StringBuffer(); 
      String line = null; 
      try { 
      BufferedReader reader = request. getReader(); 

      while ((line = reader.readLine()) != null) 
       jb.append(line); 
      } catch (Exception e) { /*report an error*/ } 

      JSONObject jsonObject = null; 
      try { 
      jsonObject = new JSONObject(jb.toString()); 
      } catch (Exception e) { 
      // crash and burn 
      throw new IOException("Error parsing JSON request string"); 
      } 
     try { 
      className = jsonObject.getString("action"); 
      methodName = jsonObject.getString("method"); 
      if(className!=null && methodName!=null){ 
      Object obj =Class.forName("com.hcl.ml.bean" +"."+ className).newInstance(); 
      //Class[] types = new Class[] {HttpServletRequest.class}; 
      Method method = obj.getClass().getMethod(methodName); 
      Object data = method.invoke(obj); 
      reply(response, data.toString()); 
      } 
     } catch (Exception e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 


    } 

    private void reply(HttpServletResponse response,String data) throws IOException{ 
     response.setContentType("application/json;charset=UTF-8"); 
     //response.setContentType("text/xml"); 
     //ServletOutputStream out = response.getOutputStream(); 
     //out.println(data); 
     PrintWriter out = response.getWriter(); 
     out.print(data); 
    } 

} 

回答

0

請務必你在你的性反應有type =>rpc

要求undefined.js因內部direct.type未解釋變量而被解僱。您應該通過輸入:rpc填充此變量並避免此錯誤。