最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - In a flask application, how can I redirect to a new page during ajax post based on condition discovered in view? -

programmeradmin5浏览0评论

In my flask application I have a sales page that collects information and saves it to the database via an ajax post request. There is a limited number of consoles available for users to record sales. A user can takeover a console from another user, but under this condition I want the initial user to lose access to the console. The console data retrieved in the view of the sales page has the information needed to determine this (available after each new sale).

I know I can’t do this with a standard “return redirect(url_for(‘dup_console’))” in the view because I’m posting through an ajax call.

I’ve spent a lot of time searching through SO and have found solutions that suggest performing the conditional logic in the success portion of the ajax call

Example:

success: function(data) {
    if (data.redirect) {
        window.location.href = data.redirect;
    }
}

I’m not sure if this will work in my case as I can’t figure out how to get the console data from the view to the success routine. When I check the response information from the success routine it contains the html from the page.

Is there a way to get the information I need from the view to the success routine of the ajax post?

Here is a portion of my code (I've tried to reduce it to just the relevant pieces):

View - sales_console

@main.route('/sales_console/<int:id>', methods=['GET', 'POST'])
@login_required
def sales_console(id):
    product_data = get_product_data()
    console_data = get_console_data(id)
    if request.method == 'POST':
        returned_data = json.dumps(request.get_json())
        returned_data_dict = ast.literal_eval(returned_data)

        <review returned data and store in database>

        <retrieved data indicates if console id used by other device>
        if duplicate_console:
            redirect to page indicating console loss (I know I can’t do it like this)

return render_template('sales_console.html', product_data=product_data, console_data=console_data)

Java Script routine available to sales_console.html template

function record_sale(event,track_sales_data,console_number)
{
    var sales_data_json = JSON.stringify(track_sales_data);
    var url = "/sales_console/"+console_number;
    $.ajax({
        url: url,
        type : "POST",
        data: sales_data_json,
        contentType:"application/json",
        success: function(response) {
            console.log("success!!!");
            console.log(response);
        },
        error: function(error) {
            console.log("failure!!!");
            console.log(error);
        }
    });
};

In my flask application I have a sales page that collects information and saves it to the database via an ajax post request. There is a limited number of consoles available for users to record sales. A user can takeover a console from another user, but under this condition I want the initial user to lose access to the console. The console data retrieved in the view of the sales page has the information needed to determine this (available after each new sale).

I know I can’t do this with a standard “return redirect(url_for(‘dup_console’))” in the view because I’m posting through an ajax call.

I’ve spent a lot of time searching through SO and have found solutions that suggest performing the conditional logic in the success portion of the ajax call

Example:

success: function(data) {
    if (data.redirect) {
        window.location.href = data.redirect;
    }
}

I’m not sure if this will work in my case as I can’t figure out how to get the console data from the view to the success routine. When I check the response information from the success routine it contains the html from the page.

Is there a way to get the information I need from the view to the success routine of the ajax post?

Here is a portion of my code (I've tried to reduce it to just the relevant pieces):

View - sales_console

@main.route('/sales_console/<int:id>', methods=['GET', 'POST'])
@login_required
def sales_console(id):
    product_data = get_product_data()
    console_data = get_console_data(id)
    if request.method == 'POST':
        returned_data = json.dumps(request.get_json())
        returned_data_dict = ast.literal_eval(returned_data)

        <review returned data and store in database>

        <retrieved data indicates if console id used by other device>
        if duplicate_console:
            redirect to page indicating console loss (I know I can’t do it like this)

return render_template('sales_console.html', product_data=product_data, console_data=console_data)

Java Script routine available to sales_console.html template

function record_sale(event,track_sales_data,console_number)
{
    var sales_data_json = JSON.stringify(track_sales_data);
    var url = "/sales_console/"+console_number;
    $.ajax({
        url: url,
        type : "POST",
        data: sales_data_json,
        contentType:"application/json",
        success: function(response) {
            console.log("success!!!");
            console.log(response);
        },
        error: function(error) {
            console.log("failure!!!");
            console.log(error);
        }
    });
};
Share Improve this question asked Jun 21, 2017 at 17:12 mulmul 381 silver badge8 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

In python, return whatever path you want to redirect on success as part of the json object you're returning, like this:

from flask import jsonify

# ...

if request.method == 'POST':
  # ...
  return jsonify(dict(redirect='path'))

In javascript, assign the returned path to the location.href property, to trigger the redirect. Like this:

// ...
success: function(response) {
  if (response.redirect) {
    window.location.href = response.redirect;
  }
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论