Search code examples
javascriptajaxcherrypy

passing db url as ajax parameter


I have a controller written in python cherrypy that should check if a db url is valid by attepting to make a connection. I'm having problems however passing the parameter to the method. My ajax call is:

    $.ajax({ async : false,
        type: 'POST',
        url: "/settings/check_db_url/"+db_url ,
        success: function(r) { alert(r) },
        error: function(r) { alert('failure') }
    });

Now the problem the urls that I need to test are in the form of:

'sqlite':'sqlite:///Users/Home/tvb-database.db' or
'postgresql+psycopg2://postgres:root@127.0.0.1:5432/tvb?user=postgres&password=postgres'

For the sqlite part I managed to pass it if I do something like:

    db_url = db_url.split('/').join('__')
db_url = db_url.split(':').join('x_xxx_x')

And then replace back in python. But this seems so hacky to me and for the postgress part I guess I'll have to replace some more. So what is the correct way to handle this?


Solution

  • Send it as part of a POST parameter and not as part of the url using the data option:

    $.ajax({ 
        async : false,
        type: 'POST',
        url: '/settings/check_db_url',
        data: { db_url: db_url },
        success: function(r) { alert(r) },
        error: function(r) { alert('failure') }
    });
    

    And in your server read the db_url POST parameter.