blosche blosche - 6 months ago 26
Javascript Question

Two different ajax urls return the same data

I am using jQuery ajax calls on my django site to populate the options in a select box which gets presented in a modal window that pops up (bootstrap) to the user. Specifically, it provides a list of available schemas in a particular database. The ajax url is dynamic which means it includes a db_host and a db_name in the URL. I have a site with clients and each client has to point to a different host/db.

The db_host and db_name passed via the URL are used in the django view to execute the necessary sql statement on the relevant host/db.

So, the URL should be unique for each db_host/db_name combination.

I am running into a problem where I am on client A's page, I click the button to display the modal. The ajax call is made. I get the data I expect. Everything is fine. Let's say the ajax URL is "/ajax/db_host/server_a/db_name/client_a_db/schema_dropdown".

Now, I got to client B's page and click the same button to display the modal. The ajax call is made. Let's say this time, the ajax URL is

However, the data returned is the actually the data that was returned for the previous ajax call I made ("/ajax/db_host/server_a/db_name/client_a_db/schema_dropdown") and not for the URL (specifically host/db) that I just passed.

I have double/triple checked that my URL is, in fact different, each time. Any help would be appreciated.

Here is my javascript function that gets called just before displaying the modal that populates my select.

function populate_schema_dropdown(db_host, db_name) {

var ajax_url = "/ajax/db_host/" + db_host + "/db_name/" + db_name + "/schema_dropdown"

url: ajax_url,
success: function (data) {
$.each(data, function (i, v) {
$("#schema-element").append("<option value='" + v[0] + "'>" + v[1] + "</option>")


Below is my django view.

def ajax_schema_dropdown(request, db_host, db_name):

cursor = get_temporary_db_connection(db_host, db_name).cursor()

FROM [sys].[schemas]
data = [(each[0], each[0]) for each in cursor.fetchall()]
return HttpResponse(json.dumps(data), content_type="application/json")

Below is get_temporary_db_connection function...

def get_temporary_db_connection(db_host, db_name):

temporary_name = str(uuid.uuid4)
connections.databases[temporary_name] = {
'ENGINE': 'sqlserver_ado',
'NAME': db_name,
'HOST': db_host,
'USER': '',
'provider': 'SQLNCLI11'

return connections[temporary_name]


Likely the problem in the way of setting temporary_name variable. It's not dynamic like it suppose to be ( I guess ), that's same database as first one are being used for all proceeding ajax calls, to fix it you have to replace line:

temporary_name  = str(uuid.uuid4)


temporary_name  = str(uuid.uuid4()) # note i've changed uuid.uuid4 -> uuid.uuid4()

Note: Likely it's make sense close connection to database after working with it, otherwise you will end up with a lot of connections in connections dict. As an option you can keep connection to db in connections dict after it was created and reconnect ( if some condition occured) or simply return existed connection without performing new connection.