2

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);
    }
});

1 Answer 1

2

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);
    }
});
Sign up to request clarification or add additional context in comments.

5 Comments

It seems that response.responseText has been returning undefined. I've edited the question to add more details. Am I using the wrong variable?
I figured it out! response.responseText doesn't exist, but my JsonP variables do. It seems that the extra JSON.parse is only necessary for other types or requests. Thanks for the help!
glad that you figured it out and can you update my answer with what you figured.. so that it helps others or update your question with answer
I'll edit the question, that way I can copy and paste my new code and the differences will be easier to see.
I can also update your answer, in case that helps someone else later.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.