user1465710
user1465710

Reputation: 1

Google app engine: getting javascript variable(client side) to python string(server side)

1)i have printed links after processing on the screen as follows-

for i in index:
   self.response.out.write('<a id="w3s" href="' + i + '">' +i+' class="testClick" </a><br>')

where index is list of processed urls. The url where results are displayed is like mysite.com/sign

2)i want to get which link user clicked. This is what i tried to do-

<script>
var addressValue;
$(document).ready(function(){
  $("a").click(function(){
    addressValue = $(this).attr("href");
  });    
});
</script>

I have got the clicked url in addressValue variable but what should i do next. I could not get how to use GET or POST methods to get the variable server side; and then how to get the string in my python variable.

Using GET/POST:(Not Working)

<script>

var addressValue;

$(document).ready(function(){
  $("a").click(function(){
    addressValue = $(this).attr("href");

//Method-I
    $.post("https://mysite.com/sign",
    {
        fooParameter: addressValue,
    },
    function(data,status){
        alert("Data: " + data + "\nStatus: " + status);
    });
  });    
});


//Method-II
$.ajax({
    url: "https://mysite.com/sign",
    method: "POST",
    data: {fooParameter: addressValue},
});

</script>

And then accessing variable in python string through following command

selectedlink=self.request.get('fooParameter')

Where am i mistaking or is there any other way? Note: I am using Google App engine

Upvotes: 0

Views: 411

Answers (1)

voscausa
voscausa

Reputation: 11706

There is another solution, which does not need javascript. Create a link to your own handler, which redirects to the query string.

Example:

<a target="_blank" href="/redir_url?http://www.example.com/address.html">Example address</a>

The Handler:

class RedirUrl(webapp2.RequestHandler):

    def get(self):

        url = self.request.query_string
        logging.info('user clicked ' + url)
        self.redirect(url)        

Upvotes: 1

Related Questions