@hook和@route没有被执行

时间:2015-06-22 18:37:36

标签: python ajax bottle beaker

我将AJAX请求发送到pythonscript“scripts / serverscript.py”,出于某种原因@hook('before_request')和 @route('scripts / serverscript.py')没有执行?有关如何调试此问题并解决问题的任何输入?

AJAX电话: -

    $.ajax({
                dataType: "json",
                type: "POST",
                url: "scripts/serverscript.py",
                //data: data,
                data: JSON.stringify(data_SU),
                error   : function (xhr, ajaxOptions, thrownError){
                    alert(xhr.status);
                    alert(thrownError); 
                    console.log("error return for server");
                },

serverscript.py

#!/usr/bin/python

import bottle
from beaker.middleware import SessionMiddleware
from bottle import hook,route
import sys
import json

result = {'success':'true','message':'The Command Completed Successfully'}
myjson = json.load(sys.stdin)
print 'Content-Type: application/json\n\n'
print json.dumps(result)

session_opts = {
    'session.type': 'file',
    'session.data_dir': './session/',
    'session.auto': True,
}

app = SessionMiddleware(bottle.app(), session_opts)

@hook('before_request')
def setup_request():
    result['message'] = r'insetup result'
    print 'Content-Type: application/json\n\n'
    print json.dumps(result)
    request.session = request.environ['beaker.session']

@route('scripts/serverscript.py')
def index():
    result['message'] = r'in - index'
    data = { 'procesing': "in index" }        
    print json.dumps(data)
    if 'processing' in request.session:
        data = { 'procesing': request.session['processing'] }        
        print json.dumps(data)
        return data

    processor()

def processor():
    result['message'] = r'in - processor'
    request.session['processing'] = 1
    #print "in processor"
    # Do some processing here for the first request
    # When processing is done you can clear "state" variable in session
    cherrypick.cp_main()
    del request.session['processing']
    request.session.modified = True

0 个答案:

没有答案