-1

User pushes the button on site and by this ajax request starts than Server returns True or False. If the result is True than another ajax request is to be processed, but I am getting nothing (I guess inside of ajax).

Here is my js code:

document.getElementById('next_in').onclick = function (){

    $.ajax({
        data: {
            login: document.getElementById('log').value,
            password: document.getElementById('pass').value
        },
        type: 'POST',
        url: '/user_login',
        success: function (localRes) {
            if(localRes.result==true){
                $.ajax({
                    data: {
                        login: document.getElementById('log').value
                    },
                    type: 'POST',
                    url: '/private',
                    success: function () {
                        alert('Connected')
                    },
                    error: function () {
                        alert('There is a mistake!')
                    }

                });
            }
            else{
                alert('Incorrect login or password!');
            }
        }
    });
}

and python code:

@app.route('/private', methods=['POST'])
def private():
   return render_template("rates.html")

Then after pushing the button on site I recieved "Connected", but then (I supposed this event calls my python function) there is no redirect to rates.html...

I do not understand what is wrong here..

Please. I hope at leaste to understand problem of which side it is and how to fix it?

Thank you!

EDIT ONE

I did shorten my python function just to show the issue. In actual case before return render_template("rates.html") there is huge proccessing (request to database, some calculation and so on), so python function is:

@app.route('/private', methods=['POST'])
    def private():
       # ******************** HUGE processing
       return render_template("rates.html")

Sorry, if I confused you, but simple redirect to .html is not what I want. I want calling python function in nested ajax requests.

Ivan
  • 41
  • 1
  • 1
  • 10

1 Answers1

1

When you use an AJAX request, the browser doesn't automatically redirect you, or display any content that is returned from your request.

If rates.html is a full HTML page, change your inner callback from

success: function () {
  alert('Connected')
},

to this:

success: function(data) {
  document.body.innerHTML = data;
},

That takes the response from the server (your python code), and then does something with it (in this case renders it on the browser).

AnilRedshift
  • 7,937
  • 7
  • 35
  • 59
  • As an aside, you should use the `done` and `fail` promises from ajax. Success and error are deprecated (although not related to your current issue) – AnilRedshift Jan 16 '18 at 19:19
  • Thank you for reply! But this is not the exact solution for me. Please see my edit one – Ivan Jan 16 '18 at 19:26