Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.0.6 API] JSON Response Parameter does not follow DataType Specified #2713

Closed
Airforce111 opened this issue Jul 23, 2015 · 6 comments
Closed
Milestone

Comments

@Airforce111
Copy link

Hellos~

When using the API Feature and routing through $.fn.api.settings.successTest, the onComplete() & onFailure() json response param does not match the datatype specified (tested for json).

To explain further... when the successTest fails, the onFailure() json response is a textual json string and not in object notation. Also, onComplete() json response is a parent object instead of the expected ajax response object.

Conversely, when successTest succeeds, the onComplete() and onSuccess() json response is in the expected object notation.

Also, this issue required me to use fiddle's /echo/ ajax service, as using SUI's built-in mock feature, gives the correct object notation in all cases. I'm thinking this is an issue in the coding related specifically to the ajax handler. (wild guess ^o^;;).

FYI. Example displays output to console...
Fiddle: http://jsfiddle.net/Airforce111/sv13cujv/

For reference...

<div id="submit_test" class="ui labeled icon primary ok button">
    <i class="edit icon"></i> Click Me!
</div>
body {
    padding: 1em;
}
$.fn.api.settings.successTest = function(response) {
    if (response && response.success) {
        return response.success;
    }
    return false;
};

$.fn.api.settings.api = {
    'create general test' : '/echo/json/',
};

$('#submit_test').api({
    method: 'POST',
    action: 'create general test',
    urlData: {},
    //data: {},
    dataType: 'json',
    beforeSend: function(settings) {
        // Create POST Data.
        settings.data = {
            json: JSON.stringify({
                success: false,    // Change this to true|false
                message: 'message',
                data: {
                    errors: ['error_code_1', 'error_code_2']
                }
            }),
            delay: 1
        };
        return settings;
    },
    beforeXHR: function(xhrObject) {},
    onComplete: function(json, element) {
        console.log('onComplete');
        console.log(json, 'OK! Is an Object, but watch contents on success/fail.');
        console.log(json.data, 'Data Depends on onSuccess/onFailure');
        alert('onComplete!');
    },
    onSuccess: function(json, element) {
        console.log('onSuccess');
        console.log(json, 'OK! Is an Object.');
        console.log(json.data, 'Data Available');
        alert('onSuccess!');
    },
    onFailure: function(json, element) {
        console.log('onFailure');
        console.log(json, 'Failed! Should be an Object.');
        console.log(json.data, 'Data Failed');
        alert('onFailure!');
    },
    onError: function(errorMessage, element) {},
    onAbort: function(errorMessage, element) {}
});
@jlukic
Copy link
Member

jlukic commented Jul 23, 2015

onFailure pulls the response object from xhr.responseText in the mockResponse cases this is a JS object literal and doesnt need to be decoded.

In the case of a real XHR it looks like responseText is not parsed by JSON.parse.

Fix incoming

@jlukic
Copy link
Member

jlukic commented Jul 23, 2015

Your example should now be fixed.

@jlukic jlukic added this to the 2.1 milestone Jul 23, 2015
@Airforce111
Copy link
Author

Thanks for the quick fix. ^o^ I'll keep using my JSON Parse workaround until the bug fix is in stable.

Just as a follow up... did you check to see if the onComplete() gives the proper response, as in the fiddle, when the successTest fails... it appears to return the full xhr, instead of a parsed xhr.responseText.

@andrejlamov
Copy link

👍

Just thought about this and were about to open an issue :). Semantic is awesome!

jlukic added a commit that referenced this issue Jul 23, 2015
jlukic added a commit that referenced this issue Jul 23, 2015
@jlukic
Copy link
Member

jlukic commented Jul 23, 2015

Thanks, it's clear to me now the inconsistencies between responses. I've also added a third parameter xhr for all callbacks, in case this needs to be used in a callback

Example of fixed with success response
http://jsfiddle.net/6esh2sn9/

With failure response
http://jsfiddle.net/g04vbtds/

@jlukic jlukic closed this as completed Jul 23, 2015
@Airforce111
Copy link
Author

=D!

@jlukic jlukic modified the milestones: 2.1, 2.0.7 Jul 23, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants