EXT种FORM的AJAX提交问题,知道的帮个忙。

beingchou 2008-12-11
  var fp = new Ext.FormPanel({
        id :  'id',
        border : false,
buttonAlign : 'right',
layout : 'column',
bodyStyle : "padding:10px",
autoWidth :  true,
        items       :item1,
buttons: [{
            text        : '查询',
            handler    : function(){
                            fp.getForm().submit({
                                waitMsg     : 'loading',
                                url         : 'index.jsp',
                                method      : 'POST',
                                success : function(form, action) {
                                        alert("OK");
                                    },
                                failure : function(form, action) {
                                        Ext.Message.alert('Error',action.result.msg);
                                    }
                            });
                          }
                }]
    });
表单内容省略。
因为跳转页面的提交是成功的。
接受的JSP如下。
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
         {success:true}
    </body>
</html>
我在里面直接写了SUCCESS,火狐DEBUG报错
错误如下
syntax error
[Break on this error] <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"\n
高手帮我一下。。。。。。。。
以后郁闷好几天了。。。。。。。。。
字符集全部是UTF-8

vison_ruan 2008-12-11
返回不能这样写

应该这样写:

<%@ page contentType="text/json; charset=utf-8" language="java"
import="java.io.*,net.sf.json.*"
%><%
Object jobj = request.getAttribute("json");
Object jsonObject = null;
if (jobj instanceof JSONObject) {
jsonObject = (JSONObject)request.getAttribute("json");
}else {
jsonObject = (JSONArray)request.getAttribute("json");
}

boolean scriptTag = false;
String cb = request.getParameter("callback");
if (cb != null) {
    scriptTag = true;
    response.setContentType("text/javascript");
} else {
    response.setContentType("application/x-json");
}
if (scriptTag) {
    out.write(cb + "(");
}
out.write( jsonObject.toString() );
if (scriptTag) {
    out.write(");");
}
%>
cwocwo 2008-12-12
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
         {success:true}
    </body>
</html> 

里的html的代码全部去掉,只在页面里写:
{success:true}
leonelwong 2009-01-09
直接写{success:true}
这样就没有错误了
applebomb 2009-02-15
楼上都说完了,总结归纳一下:Ext的Form的submit接收的是JSON数据,而不是HTML。

所以返回个{}就够了,API如是说:

doAction( String/Object actionName, [Object options] ) : BasicForm

.....

success : Function
The callback that will be invoked after a successful response. Note that this is HTTP success (the transaction was sent and received correctly), but the resulting response data can still contain data errors. The function is passed the following parameters:

也就是说,有HTTP返回,就认为正确。不过EXT应该是多做了个处理,判断了一下返回对象是否是json,所以就导致你那个错了。

多翻翻API,就明了了,呵呵...
Global site tag (gtag.js) - Google Analytics