2

I am in the middle of creation of a web application.

In javascript, I make a ajax call to my python script, process and send the result back as response. /process/ is the route to the python script for the processing of values which are sent as json, namely value1 and value2. I send back response as json setting value in key 'result'.

In the success block of ajax, the value is stored in name_value and this should be passed as parameter to the python routing method.

Javascript:

$.ajax({
        type: "GET",
        url: "/process/",
        contentType: "application/json; charset=utf-8",
        data: { "value1" : value1,
                "value2" : value2
               },
        success: function(data) {
            var name = data.result;
            console.log(name);
            window.location.href = "{{url_for('/process2', name=name)}}"
        }
    });

Python script:

app.route("/process2/<name>")
def process2(name):
    print name
    render_template("user.html", name=name);

I am not receiving the parameter value in this case if parameter is passed in url_for. If I hardcode the parameter, I am able to receive the parameter value in the python script as in,

window.location.href = "{{url_for('/process2', name='helloworld')}}"

Any help is appreciated. Thanks in advance.

2
  • 1
    Is that JS being rendered in the template or is it in a static file? The only way that url_for will be turned into the actual URL string by Jinja is if it's part of your template. Commented Nov 24, 2014 at 21:17
  • Yes.. its rendered in the template only. Template folder contain my html file within which I have my javascript. Commented Nov 24, 2014 at 21:29

1 Answer 1

1

From experience, I have learned that Jinja cannot utilize Javascript variables, like the following:

window.location.href = "{{ url_for('/process2', name=name) }}"

If I am wrong about that, somebody please correct me and you shall become my best friend in the SO community.

EDIT:

As Jason Heine mentioned, you can achieve what you want by passing in the url_for method as the result in the json:

Python:

from flask import jsonify, url_for

@app.route("/process", methods=['GET', 'POST'])
def process():    
    name = 'bubba'  # code to get the name
    return jsonify({
        'result': url_for('process2', name=name),
        'name': name
     })

@app.route("/process2/<string:name>")
def process2(name):
    print name
    render_template("user.html", name=name);

Javascript:

$.ajax({
    type: "GET",
    url: "/process",
    contentType: "application/json; charset=utf-8",
    data: { "value1" : value1,
            "value2" : value2
           },
    success: function(data) {
        var route = data.result;
        var name = data.name;
        console.log(route, name);

        window.location.href = route;
    }
});

You may also check out the AJAX with jQuery documention if you have any more questions.

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

2 Comments

Or, you could just do the url_for in the route "process" and return it to the ajax call, pull that data, and then pass it to the location.href.
Ah! Why didn't I think of that? Thanks for the information

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.