0

$.ajax({

type: "PUT",
url: url,
data: data,
dataType: 'json',
success: function(data) {
  alert("success");


},
error: function(data) {
  var errors = data.responseJSON;
  console.log(errors);
}

})
});

the success event get triggered even though the post call fails (validation error), when does the error function get called?. what did i do wrong in that?

8
  • try changing url: 'url, to url:url, . Hope this will help. Commented Jan 31, 2018 at 5:33
  • try to send response status with status: :unprocessable_entity when error occurs Commented Jan 31, 2018 at 5:34
  • @SushantPimple that's a typo here. i have edited now Commented Jan 31, 2018 at 5:35
  • Your are sending errors with status code 200. Thats why jquery treats it as success. send validation errors with status code 400 or something Commented Jan 31, 2018 at 5:35
  • @Vishal thank u . that's what the mistake is Commented Jan 31, 2018 at 5:38

2 Answers 2

1

when validation fails you need to send response status with status: :unprocessable_entity so it will go in error event in ajax call

Sign up to request clarification or add additional context in comments.

Comments

0

The required parameters in an Ajax error function are jqXHR, exception, use it like below :

  $.ajax({
    type: "PUT",
    url: url,
    data: data,
    dataType: 'json',
    success: function(data) {
        console.log(data);
    },
    error: function (jqXHR, exception) {
        console.log(jqXHR);
    }
  });

Comments

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.