I am having an issue with Python Bottle and the jQuery AJAX function. Right now, when I try to use the AJAX call after a button submission call (via the jQuery.submit()
function), the data that is passed back is the entire HTML text.
On the Bottle side, I have code that looks as such:
@route('/testing')
@view('test.tpl')
def test():
myDict = request.query.decode()
data = 100
a = 1
b = 2
c = []
if myDict:
c.append(myDict['var1'])
c.append(myDict['var2'])
return json.dumps(c)
return locals()
On the JS side, I have code that looks as such:
$('#buttonSubmit').submit(function(d) {
e.preventDefault();
$.ajax({
type: 'GET', // I get that this is redundant
// dataType: 'json',
url: '/testing?var1=test1&var2=test2',
success: function(data) { console.log(data); }
});
});
On the HTML side, I have code that looks as such:
<form id='buttonSubmit'>
<button type='submit'>Go</button>
</form>
My assumption is that when I click the button, it makes the AJAX call, which goes to the Python file via the Bottle interface. In this .py file, it will then make the test()
call and return a dictionary of the local values (via locals()
). However, when I log into console the 'data' object that is passed back, it turns out to be the entire HTML file (including tags).
My question is: why is this the case? I have figured out that the responseText for the jqXHR object matches the data that is given to the success function callback.
EDIT:
Let me explain the general flow of this. I begin by loading the /testing
page. Upon doing so, the test.tpl
template file renders fine and the Python file returns the locals()
dictionary. However, after this page is loaded, when an AJAX call is fired, I have it go to this same test()
Bottle method with a query. In response to this AJAX query, locals()
will still return the same data, but the data that will be returned to the JS (the data belonging to the success
callback function) will now be the entire HTML text, as opposed to a dictionary.
However, if I were to change this and have a separate return (i.e. return json.dumps(c)
), then the data registered will be a JSON file (once I add in the dataType='json'
part. So I guess herein lies my confusion. What is different about AJAX that causes this result?