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

ajax success/error handlers #23

Closed
devrow opened this issue Feb 10, 2014 · 4 comments
Closed

ajax success/error handlers #23

devrow opened this issue Feb 10, 2014 · 4 comments

Comments

@devrow
Copy link

devrow commented Feb 10, 2014

hi.
something is wrong...
(for all requests server status is 200)

var client = new $.RestClient('/rest/api/', { stringifyData: true });
restAjaxOptions = {
    success: function(data, textStatus, jqXHR) {
       console.log("success " + jqXHR);
    },
    error: function(jqXHR, textStatus, errorThrown) {
       console.log("error " + jqXHR);
    }
};
client.add('items', {ajax: restAjaxOptions});
...
$("button[data-role='save']").on("click", function(e){
  client.items.update($("#form").serializeJSON());
});

console log:

success [object]
var client = new $.RestClient('/rest/api/', { stringifyData: true });
restAjaxOptions = {
    success: function(data, textStatus, jqXHR) {
       console.log('success ' + jqXHR);
    },
    error: function(jqXHR, textStatus, errorThrown) {
       console.log('error ' + jqXHR);
    }
};
client.add('items');
...
$("button[data-role='save']").on("click", function(e){
  client.items.update($("#form").serializeJSON(), {ajax: restAjaxOptions});
});

console log:

success undefined
error undefined

how can i pass the error handlers for each resource request?
thx.

@jpillora
Copy link
Owner

Do you want handlers that run for all requests under one resource? or do you want handlers for one particular request?

@devrow
Copy link
Author

devrow commented Feb 10, 2014

for one particular request. mainly for update resource requests. but for GET method i want handle internal server side script errors contains in data argument.

for example:

restAjaxOptions = {
    success: function(data, textStatus, jqXHR) {
       if (data.rc == -1) {
           alert("data too big");
       } else {
           // bla-bla-bla
       }
    },
//...
};

maybe there is another way?

@jpillora
Copy link
Owner

So update(id1, id2, idN, data) only accepts IDs (to build the URL) and the POST/PUT body, though it returns a deferred object (a jquery promise). So what you want is:

client.foo.update({test:42}).done(successHandler);
client.foo.update({test:42}).fail(errorHandler);
//where errorHandler is function(xhr, result, statusText){...}

@devrow
Copy link
Author

devrow commented Feb 10, 2014

ok. thank you. 👍
i casually read the manual.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants