Search code examples
javascriptxmlformsextjsresponse

Error on XML response of a form submit in ExtJS 5


I am currently upgrading a form to ExtJS 5. It is supposed to send a request to a webservice that responds in XML. Since ExtJS expects JSON by default, I set up a custom errorReader according to the documentation:

errorReader: new Ext.data.XmlReader({
    record: 'error',
    success: '@success'
    }, [
        'id', 'msg'
    ]
)

Raw response of the server is:

HTTP/1.0 200 OK
Content-Type: text/xml
Content-Length: 150
Expires: Mon, 20 Oct 2014 10:00:53 GMT

<?xml version="1.0" encoding="Windows-1252"?>
<message success="false"><errors><error><id>-1</id><msg>FieldMissing</msg></error></errors></message>

When the response comes in, I see an error in my page like this (translated from German):

Error: Property "scheme" can not be obtained from an undefined or null referenced object
Row: 48503
Source: ext-all-debug.js

It happens in an "extractData"-function that does the following:

entityType = readOptions && readOptions.model ? Ext.data.schema.Schema.lookupEntity(readOptions.model) : me.getModel(),
schema = entityType.schema,

readOptions is undefined, so it does me.getModel() which returns undefined, though I defined a model in the errorReader with id and msg. Then entityType is undefined leading to the error stated above.

I don't understand, what I am doing wrong. Please help. Thanks in advance :)


Solution

  • The best questions are the ones I can answer myself ;) It seems like the documentation is wrong or there is a bug in the current implementation. What the docs said, is just not working:

    errorReader: new Ext.data.reader.Xml({
        record : 'field',
        success: '@success'
    }, [
        'id', 'msg'
    ])
    

    You have to explicitly (not anonymous) define an appropriate model:

    Ext.define('Field', {
        extend: 'Ext.data.Model',
        fields: ['id', 'msg']
    });
    

    ... and then assign it in the form:

    errorReader: new Ext.data.reader.Xml({
        record : 'field',
        model: 'Field',
        success: '@success'
    })
    

    There seems to be another problem though.. Setting the success property doesn't work at all! I can write 'foo' and it still defaults to 'success', which means it always expects a child of the root instead of an attribute of the root.