Results 1 to 2 of 2

Thread: How to read Json reponse from Ajax Request

  1. #1
    Sencha User kitesurf's Avatar
    Join Date
    Jan 2012
    Location
    London, England
    Posts
    78
    Answers
    4
    Vote Rating
    7
      1  

    Default Answered: How to read Json reponse from Ajax Request

    I am submitting form values via an ExtAjax.request:

    Code:
            Ext.Ajax.request({
                url: proxyPrefix+"/signon",
                method: 'POST',
                params: theForm.getValues(),
                scope: this,
                waitTitle: 'Signing on',
                waitMsg: 'Connecting...',
                success: function(response) {
                    loadingMask.hide();
                    // We got a response from the backend, check to see if we logged in ok.
                    var returnInfo = Ext.decode(response.responseText);
                    console.log("controllers.SignOnController - successful logon");
                    Ext.dispatch({
                        controller: 'Accounts',
                        action: 'index',
                        historyUrl: 'Accounts/index'
                    })
                },
                failure:function(response) {
                    loadingMask.hide();
                    Ext.Msg.alert('Login Failed: ', response.errorMessage);
                }
            })
    I use Ext.decode to get the responseText from the response and put this into returnInfo:

    Code:
    • returnInfo: Array[1]
      • 0: Object
        • data: Object
          • returnCode: "10"
          • __proto__: Object
        • success: "false"
        • __proto__: Object
      • length: 1
      • __proto__: Array[0]
    Can anyone tell me how I go about getting the values in returnInfo? Ideally I want to check the value of 'success' as well as what we have in 'data'.

    Perhaps there is a neater way of doing what I need?

    thank you!

  2. Seems I'm in the habit of answering my own questions. I managed to figure this out by first decoding the responseText which returns an Array and then reading the first line of the Array. Probably not best way of doing things, but this worked for me. If anyone else has a better suggestion, please chime in.
    Code:
                Ext.Ajax.request({
                    url: proxyPrefix+"/signon",
                    method: 'POST',
                    params: theForm.getValues(),
                    scope: this,
                    successProperty: 'success',
                    waitTitle: 'Signing on',
                    waitMsg: 'Connecting...',
                    success: function(response) {
                        loadingMask.hide();
                        // We got a response from the backend, check to see if we logged in ok.
                        var returnInfo = Ext.decode(response.responseText);
                        if (returnInfo[0].success == "false") {
                            console.log("controllers.SignOnController - logon failed");
                            this.logonFailed();
                        } else {
                            console.log("controllers.SignOnController - successful logon");
                            Ext.dispatch({
                                controller: 'Accounts',
                                action: 'index',
                                historyUrl: 'Accounts/index',
                                calledFrom: 'signon'
                            })
                        }
                    },
                    failure:function(response) {
                        loadingMask.hide();
                        this.serviceUnavailable();
                    }
                })
    My JSON response is:
    Code:
    "[{"success":"true","data":{"returnCode":"0"}}]"

  3. #2
    Sencha User kitesurf's Avatar
    Join Date
    Jan 2012
    Location
    London, England
    Posts
    78
    Answers
    4
    Vote Rating
    7
      1  

    Default

    Seems I'm in the habit of answering my own questions. I managed to figure this out by first decoding the responseText which returns an Array and then reading the first line of the Array. Probably not best way of doing things, but this worked for me. If anyone else has a better suggestion, please chime in.
    Code:
                Ext.Ajax.request({
                    url: proxyPrefix+"/signon",
                    method: 'POST',
                    params: theForm.getValues(),
                    scope: this,
                    successProperty: 'success',
                    waitTitle: 'Signing on',
                    waitMsg: 'Connecting...',
                    success: function(response) {
                        loadingMask.hide();
                        // We got a response from the backend, check to see if we logged in ok.
                        var returnInfo = Ext.decode(response.responseText);
                        if (returnInfo[0].success == "false") {
                            console.log("controllers.SignOnController - logon failed");
                            this.logonFailed();
                        } else {
                            console.log("controllers.SignOnController - successful logon");
                            Ext.dispatch({
                                controller: 'Accounts',
                                action: 'index',
                                historyUrl: 'Accounts/index',
                                calledFrom: 'signon'
                            })
                        }
                    },
                    failure:function(response) {
                        loadingMask.hide();
                        this.serviceUnavailable();
                    }
                })
    My JSON response is:
    Code:
    "[{"success":"true","data":{"returnCode":"0"}}]"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •