Jquery Error Http Status Code
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta jquery ajax http status code Discuss the workings and policies of this site About Us Learn more jquery ajax status code 0 about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack ajax error codes list Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you,
Ajax Error Code 200
helping each other. Join them; it only takes a minute: Sign up jQuery: How to get the HTTP status code from within the $.ajax.error method? up vote 34 down vote favorite 5 I'm using jQuery to make an AJAX request. I want to perform different actions whether or not the HTTP status code is a 400 error or ajax status code list a 500 error. How can I achieve this? $.ajax({ type: 'POST', url: '/controller/action', data: $form.serialize(), success: function(data){ alert('horray! 200 status code!'); }, error: function(data){ //get the status code if (code == 400) { alert('400 status code! user error'); } if (code == 500) { alert('500 status code! server error'); } }, }); Update: @GeorgeCummins mentioned that it "seemed odd" to work with the response body. This is the first time that I've attempted doing this sort of thing. Is my approach not a best-practice? What would you recommend? I created another StackOverflow question for this here: What response/status code should I send to an AJAX request when there is a user/form validation error? jquery share|improve this question edited Jul 15 '11 at 0:05 asked Jul 14 '11 at 22:41 Andrew 61.8k128395586 add a comment| 4 Answers 4 active oldest votes up vote 55 down vote accepted If you're using jQuery 1.5, then statusCode will work. If you're using jQuery 1.4, try this: error: function(jqXHR, textStatus, errorThrown) { alert(jqXHR.status); alert(textStatus); alert(errorTh
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About jquery statuscode example Us Learn more about Stack Overflow the company Business Learn more about hiring
Jquery Ajax Status Code Handling
developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the
Jquery Post Http Status Code
Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How do I get the HTTP http://stackoverflow.com/questions/6700822/jquery-how-to-get-the-http-status-code-from-within-the-ajax-error-method/6700850 status code with jQuery? up vote 34 down vote favorite 9 I want to check if a page returns the status code 401. Is this possible? Here is my try, but it only returns 0. $.ajax({ url: "http://my-ip/test/test.php", data: {}, complete: function(xhr, statusText){ alert(xhr.status); } }); jquery ajax xmlhttprequest http-status-codes http-status-code-401 share|improve this question edited Mar 21 '13 at 8:53 Serge 53911027 asked Jun 2 '10 http://stackoverflow.com/questions/2955947/how-do-i-get-the-http-status-code-with-jquery at 8:09 horgen 77661831 Check the value of statusText, the second parameter to the callback function instead. –Jimmy Cuadra Jun 2 '10 at 8:16 this alerts "Authorization Required". Ican work with that, but a 401 alert would be better ;) –horgen Jun 2 '10 at 9:12 add a comment| 6 Answers 6 active oldest votes up vote 58 down vote this is possible with jQuery $.ajax() method $.ajax(serverUrl, { type: OutageViewModel.Id() == 0 ? "POST" : "PUT", data: dataToSave, statusCode: { 200: function (response) { alert('1'); AfterSavedAll(); }, 201: function (response) { alert('1'); AfterSavedAll(); }, 400: function (response) { alert('1'); bootbox.alert('Error While Saving Outage Entry Please Check', function () { }); }, 404: function (response) { alert('1'); bootbox.alert('Error While Saving Outage Entry Please Check', function () { }); } }, success: function () { alert('1'); }, }); share|improve this answer edited Sep 27 '12 at 18:21 Gung Foo 8,32422033 answered Sep 27 '12 at 17:31 Ravi Mittal 72869 1 I think this does not work now, I tried, any ideas? –Aravind Jul 27 '13 at 16:55 Its working fine. Can you share what you trying ? &ndash
here for a quick overview of the site Help Center Detailed answers to any questions http://stackoverflow.com/questions/25467158/get-http-status-code-from-an-ajax-response-with-jquery you might have Meta Discuss the workings and policies of http://stackoverflow.com/questions/12759627/jquery-ajax-http-status-code this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a status code community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Get Http Status Code from an ajax response with jquery up vote 1 down vote favorite In my current spring project, when I submit a form to server, the response is handled by http status code this method: $('form.form').each(function () { var form = this; $(form).ajaxForm(function (data) { form.reset(); $(".alert-info").find("#alert").html(data); $(".alert-info").show(); }); }); In my controller, the submission is handled by a method like this: @RequestMapping(value="cadastra", method=RequestMethod.POST) @ResponseBody @ResponseStatus(HttpStatus.CREATED) public void cadastra(@ModelAttribute("object") E object, BindingResult result, @RequestParam(value="file", required=false) MultipartFile file, @RequestParam(value="icone", required=false) MultipartFile icone, @RequestParam(value="screenshot", required=false) MultipartFile screenshot[]) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IOException { serv.cadastra(object); serv.upload_picture(object, file, "picture"); serv.upload_picture(object, icone, "icone"); } Error responses from the methods from the controller are handled by this ControllerAdvice class: @ControllerAdvice @PropertySource({"classpath:error.properties"}) public class GlobalDefaultExceptionHandler { @Autowired private Environment env; @ExceptionHandler(value = Exception.class) public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception { // If the exception is annotated with @ResponseStatus rethrow it and let // the framework handle it - like the OrderNotFoundException example // at the start of this post. // AnnotationUtils is a Spring Framework utility class. if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null) throw e; // Otherwise setup and send the user to a default err
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up jquery ajax http status code up vote 4 down vote favorite 2 I am changing my scripts to jquery at the moment. This is my old javascript: var request = (window.XMLHttpRequest) ? new XMLHttpRequest() : (window.ActiveXObject ? new window.ActiveXObject("Microsoft.XMLHTTP") : false); request.open("GET", url, true); request.onreadystatechange = updatePage; request.send(null); function updatePage() { if (request.readyState == 4) { if (request.status == 200) { var response = request.responseText; doSomething(); } else if (request.status == 304) { doSomethingElse(); } else { } } } I now want to change this to jquery-ajax: request = $.ajax({ type: "GET", url: url, data: data, success: updatePage }); How can I test for the status code (and the responseText) returned by the request, like I did in my old script? jquery ajax share|improve this question asked Oct 6 '12 at 12:05 Zulakis 1,91762042 add a comment| 2 Answers 2 active oldest votes up vote 18 down vote accepted try this: request = $.ajax({ type: "GET", url: url, data: data, statusCode: { 200: function() { doSomething(); }, 304:function(){ doSomethingElse(); } } }); or: request = $.ajax({ type: "GET", url: url, data: data, complete: function(e, xhr, settings){ if(e.status === 200){ }else if(e.status === 304){ }else{ } } }); share|improve this answer edited Sep 1 '15 at 3:33 Hugh Lee 5,58675080 answered Oct 6 '12 at 12:07 mgraph 12.4k22354 See my edit above, I really need the status code. Sorry if I didn't point that out correctly before. –Zulakis Oct 6 '12 at 12:09 @Zulakis see updated answer –mgraph Oct 6 '12 at 12:11 How am I going to get the responseText now? –Zulakis Oct 6 '12 at 12:14 @Zulakis 200: function(dataText) { or complete:...text = e.responseText –mgraph Oct 6 '12 at 12:18 1 Hmm, i tried statusCode: { 200: function() { alert('Status 200'); }, 304: function(){ alert('Status 304'); } } but it is always alerting 'Status 200' even though I can clearly see that the Status is 304 in fireb