Techwolf
Techwolf

Reputation: 1228

Get variable from Sencha Touch Ext.data.JSONP.request response

I'm trying to get a boolean (success) and a HTML string (result) from the response to an Ext.data.JsonP.request, but I can't fgure out how. Here's the code I have so far:

Ext.data.JsonP.request({
    url: 'http://wereani.ml/shorten-app.php',
    callbackKey: 'callback',
    params: {
        data: Ext.encode(values)
    },
    success: function(response) {
        console.log(response);
        console.log(JSON.stringify(values));
        console.log('Link Shortened');
        if(response.responseText['success'] == true) {
        Ext.Msg.alert('Link Shortened', response.responseText, Ext.emptyFn);
        } else {
            Ext.Msg.alert('Error', response.responseText, Ext.emptyFn);
        }
        form.reset();
    },
    failure: function(response) {
        console.log(response);
        console.log(JSON.stringify(values));
        console.log('Error');
        Ext.Msg.alert('Error', 'Please try again.', Ext.emptyFn);
    }
});

Some sample JSON-P:

callback({"success":false,"result":"<div class=\"error\">Please enter a valid link to shorten!<\/div>"})

The error I'm currently getting is Uncaught TypeError: Cannot read property 'success' of undefined. Thanks in advance for the help!

EDIT: It seems that while response is Object {success: false, result: "<div class="error">Please enter a valid link to shorten!</div>"}, response.responseText is undefined. Am I using the wrong variable? I couldn't find any documentation on response.

EDIT 2: It seems that the reason Console.log(response.responseText) was returning undefined is because it.... was. The JsonP-encoded variables (success and result) that I passed were parsed automatically into the object, and response.responseText was never created (though I think it was supposed to be). The solution was to just read response.success and response.result directly!

ANSWER: Here's the code that worked. Thanks to Viswa for the help!

Ext.data.JsonP.request({
    url: 'http://wereani.ml/shorten-app.php',
    callbackKey: 'callback',
    params: {
        data: Ext.encode(values)
    },
    success: function(response) {
        if(response.success === true) {
            Ext.Msg.alert('Link Shortened', response.result, Ext.emptyFn);
        } else {
            Ext.Msg.alert('Error', response.result, Ext.emptyFn);
        }
        form.reset();
    },
    failure: function(response) {
        console.log('Error');
        Ext.Msg.alert('Error', '<div class="error">Please try again.</div>', Ext.emptyFn);
    }
});

Upvotes: 2

Views: 4274

Answers (1)

Viswa
Viswa

Reputation: 3211

Try like this

Ext.data.JsonP.request({
    url: 'http://example.com/script.php',
    callbackKey: 'callback',
    params: {
        data: Ext.encode(values)
    },
    success : function(response) {
      console.log("Spiffing, everything worked");
      // success property
      console.log(response.success);
      // result property
      console.log(response.result);
   },
   failure: function(response) {
        console.log(response);
        Ext.Msg.alert('Error', 'Please try again.', Ext.emptyFn);
    }
});

Upvotes: 2

Related Questions