0

My Ajax post request updates the HTML on success function, but it disappears after complete function.

Here is my Ajax request:

$.ajax({
    type: 'POST',
    url: 'http://api.open-notify.org/astros.json',
    dataType: 'json',
    async: false,
    dataType: 'json',
    success: (data) => {
        alert(data.Title)
        $("#final").append("Success")
    },
    error: (data) => {
        $("#final").append("error")
    }, complete: () => {
        $("#final").append("#################")
        alert("complete")
    },
    timeout: 3000 
});

What changes should be made to make sure updated html code is shown?

1
  • can you add your html? are you getting an errors in the console? Commented May 18, 2018 at 22:44

2 Answers 2

1

You're so close!

If you right click and select "inspect", and then under "console" you'll see Ajax errors. In this case, it was returning a 405 Method Not Allowed, meaning the POST request was failing.

I've simplified your Ajax request a bit and made it work. Here's a working jsfiddle demo.

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

Comments

0

There are also some unnecessary parameters you are adding to the Ajax call, your request for instance should be a GET and not a POST.

$.ajax({
    url:'http://api.open-notify.org/astros.json',
    complete: function (response) {
        $('#output').html(response.responseText);
    },
    error: function () {
        $('#output').html('There was an error!');
    },
});

Try it here.

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.