Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # MAIN.PY
- # THIS IS THE FUNCTION URL FROM WHERE THE AJAX POST IS DONE:
- template_dir = os.path.join(os.path.dirname(__file__), 'inc/html/')
- jinja = jinja2.Environment(autoescape=True, loader=jinja2.FileSystemLoader(template_dir))
- SETTINGS_PATH = os.path.normpath(os.path.dirname(__file__))
- img_dir = os.path.join(SETTINGS_PATH, "inc/req")
- class Test(webapp2.RequestHandler):
- def get(self):
- for file in os.listdir(img_dir):
- h = []
- h.append(file)
- for l in h:
- r = l.endswith('.jpg') or l.endswith('.png') or l.endswith('.gif')
- if r == True:
- dd.append(l)
- dd.sort()
- template_values = {'images': dd}
- template = jinja.get_template('img.html') # The HTML file with AJAX.JS file
- self.response.write(template.render(template_values))
- # THIS CLASS RECEIVES THE AJAX POST
- class DoubleNumbers(webapp2.RequestHandler):
- def get(self):
- pass
- def post(self):
- self.response.headers['Content-Type'] = 'application/json'
- data = json.loads(self.request.body)
- z = data['imageSrc']
- logging.info(z)
- self.response.write(z)
- app = webapp2.WSGIApplication([
- ('/tst', Test), # AJAX POST from URL /tst to URL /i
- ('/i', DoubleNumbers),
- ], debug=True)
- # THE JS FILE
- $(document).ready(function() {
- $("button").click(function(){
- var data = {'imageSrc' : $(this).parent().find('img').attr('src')};
- var request = $.ajax({
- cache: false,
- url: "/i",
- type: "POST",
- data: JSON.stringify(data),
- dataType: "json",
- }).done(function(data){
- var txt = "<!DOCTYPE html><html><body><img src='"+data+"'></body></html>";
- document.open("text/html");
- document.write( txt );
- document.close();
- console.log(data);
- console.log(txt);
- });
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement