ajax request in extjs always executes success
I have a ajax request, where i have code for both success and failure
success: function(my_response) {
},
failure: function(my_response) {
}
I am using Perl CGI in the server to handle the Ajax request, it prints below string when there is a failure
print "Content-type: text/plain\n\n";
print "{success: false, errors: {response: 'Query Failed -- Please check the syntax'}}";开发者_如何学C
exit
In the firebug, i can see the above response. But, my browser is always executing the code in success. Is there anything wrong in the way i am handling the ajax request?
You are sending JSON message with status code 200 which is considered as success. The fact that this JSON message contains some custom structure with an error message is not something that extjs is capable of knowing. You could either send a 500 HTTP status code from your server or simply use an if condition in your success handler like this:
success: function(my_response) {
if (my_response.success) {
alert('it worked!');
} else {
alert('oops, something went wrong: ' + my_response.errors.response);
}
}
Also readapt the content type to what you are actually sending:
print "Content-Type: application/json\n\n";
精彩评论