0

I have a form submitting data and waiting for a json response but some errors/warnings/notices on PHP raises a javascript Exception when form tries to parse the responseText into JSON.

Is there a way to catch the error or perform my validation before form does?

I tried overriding Ext.JSON.decode, but for some reason form doesn't use that definition even when console error shows as follow

Uncaught Ext.JSON.decode(): You're trying to decode an invalid JSON String: my wrong json test!! ext-all.js:21
Ext.Error.Ext.extend.statics.raise ext-all.js:21
Ext.JSON.me.decode ext-all.js:21   <-- why Ext.JSON.[me].decode?
Ext.define.handleResponse ext-all.js:21
Ext.define.processResponse ext-all.js:21
Ext.define.onSuccess ext-all.js:21
Ext.apply.callback ext-all.js:21
Ext.define.onComplete ext-all.js:21
Ext.define.onStateChange ext-all.js:21
(anonymous function)

Any idea? Thanks in advance.

4

1 回答 1

7

我知道,您的评论是正确的,但有时会突然出现服务器配置或意外问题,我希望最终用户可以发送错误或至少不会破坏应用程序。我找到了解决方法。覆盖 Ext.form.action.Submit 并将非 json 响应封装到有效的 json 对象中。之后,调用原来的onSuccess

Ext.override(Ext.form.action.Submit,{
    onSuccess: function( pResponse ){
        try{
            eval( "(" + pResponse.responseText + ')' );
        }catch(e){
            pResponse.responseText = '{ success: false, info: "' + pResponse.responseText.replace(/"/g, '\\"') + '" }';
        }
        this.callParent( arguments );
}
});

它可以工作,但请确保您在应用程序中运行此脚本一次。

于 2012-11-15T01:20:20.237 回答