0

ExtdirectのRemotingAPIを使用してリモートメソッドを呼び出そうとしています。しかし、次のURLを押すとエラーが発生します: "http://10.112.202.164:8080/TestProject/app.html"

エラー: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);
    }

}
4

1 に答える 1

0

あなたが持っているものを確認してくださいtype=>rpcあなたの応答に。

undefined.js内部のdirect.type未定義変数が原因で発火するリクエスト。type:rpcを渡してこの変数を埋め、このエラーを回避する必要があります。

于 2012-09-24T10:32:28.287 に答える