Ext JS 5 about store load return JSON error message or exception handling

Source: Internet
Author: User

The server returned an error message or handled a server error when it was in store load. ExtJS4 should also be able to use, not tested.

Here are two things:

    1. The server returned an error JSON error message with a status of 200
    2. Server exception with a status of 500
first, the processing of the return JSON error message

When you get JSON data in the store, the data structure is similar:

{     "users": [{         "id": 1,         "name": "Ed",         "Orders": [{              "id": Ten,             "Total": 10.76,             "status": "Invoiced"        },{             "id": 11 ,             " Total ": 13.45,             " status ":" Shipped "        }]     }]}

This is the normal data.

However, if the server determines that the user is not logged in or does not have permission, the error message data structure We return should be:

{    false,    "error": "There was a error with your request"}

Similar to Ext.Ajax.request usage.

However, by default, the store does not handle cases where success is false or throws an exception. What about that?

Let's take a look at the configuration items when you define the store:

var New Ext.data.Store ({fields    : [' foo '],    proxy  : {        type   ' Ajax ',        URL     ' Data.json ',        reader: {            type            ' json ',            rootproperty    ' Data ',            ' ERROR '}}    );

We need to add the configuration item messageproperty in reader, and when you return data that is success false, the JSON object property value corresponding to this configuration item will be used later, and this configuration item is configured as "error".

Then we can handle the error, and we'll go back to the store and look at the store's Load event.

function (store, records, successful, eopts) {    if(!  Successful) {        var error = eopts.geterror ();    }});

There are four parameters:

Store, records, successful, eopts

Where successful is the success property corresponding to the JSON returned by the server, and the successful parameter is false when success is false. In the error attribute of eopts we can wait until the Messageproperty of the configured property values in reader.

That is, the value of the error variable is: "There was a error with your request".

The callback and load events in load are all possible, and here the operation corresponds to the eopts parameter in the event.

store.load ({    function(records, operation, success) {        if  (success) {             //  ...        Else {            var error = operation.geterror ();             Ext.Msg.alert (' ERROR ', error);}}    );

Of course, if you want to deal with different errors according to the situation, the data you send over the server is error code or object, and the front end is processed by different error values.

Second, server exception

For example, the 500 error, using the same as above, is only the error in eopts not the value you configured, but an object, which has three properties:

    • Response: Information returned by the server
    • Status: State value, if server is not found, 0, server internal error, 500
    • StatusText: Error title

The processing method can be processed according to status, Response.responsetext the text returned by the server.

This article only provides methods, details can be viewed Console.dir (eopts) output.

The complete code is as follows, and the other store inherits this class:

JavaScript
Ext.define ("HandPlm.ux.store.Base", {extend:"Ext.data.Store", constructor:function () {        varme = This;         Me.callparent (arguments); Me.on ("Load",function(store, records, successful, eopts) {if(Successful = = =false) {                varError =Eopts.geterror (); if(typeofError = = = "string") {Ext.Msg.alert (Error, Eopts.geterror ()); }                if(typeofError = = = "Object") {                    Switch(error.status) { Case0: Ext.Msg.alert ("Error", "Server not Found");  Break;  Case500: Ext.Msg.alert ("Server Internal Error", Error.response.responseText);  Break; default: Console.error (Error);    }                }            }        }); }});

Don't forget to configure messageproperty in reader

Ext JS 5 about store load return JSON error message or exception handling

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.