Reputation: 746
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?
Upvotes: 0
Views: 1885
Reputation: 18128
That route is configured to return a view, not a JSON dict. If you comment out this line, then it will return a dict instead:
@view('test.tpl')
view
renders the template only when the response is a dict. When it's a string (as in your response from json.dumps
), it just passes the result through unmodified:
% pydoc bottle.view bottle.view = view(tpl_name, **defaults) Decorator: renders a template for a handler. The handler can control its behavior like that: - return a dict of template vars to fill out the template - return something other than a dict and the view decorator will not process the template, but return the handler result as is. This includes returning a HTTPResponse(dict) to get, for instance, JSON with autojson or other castfilters.
Upvotes: 1