Python Daily
2.57K subscribers
1.48K photos
53 videos
2 files
38.9K links
Daily Python News
Question, Tips and Tricks, Best Practices on Python Programming Language
Find more reddit channels over at @r_channels
Download Telegram
Json issue with checkbox

Basically I have a form that submits notes on work orders. I've got it posting but now its not passing checkbox data no matter if the box is checked it still goes thru as if its not. It passed a "on"

> /*global $*/
$('#noteform').submit(function(e) {
e.preventDefault();

var csrf_token = "{{ csrf_token() }}";
var data = {};
var Form = this;

$.each(this.elements, function(i, v) {
var input = $(v);
data[input.attr("name")] = input.val();
delete data["undefined"];
});

$.ajax({
type: 'POST',
url: '/ticket/add/note',
dataType: 'json',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(data),
context: Form,
success: function(callback) {
console.log(callback);
$('#notes').load(' #notes');
$('#notecount').load(' #notecount');
$('input[type=text], textarea').val('');
$('input[type=checkbox]').prop("checked", false);
},
error: function() {
alert('error posting!');
}
});

$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!/^(GET|HEAD|OPTIONS|TRACE)$/i.test(settings.type) && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken", csrf_token);
}
}
});

});

/r/flask
https://redd.it/7g1jsm
json post issue

Ok, so here's the problem. I have a Notes form. It posts the information properly but lets say I'd like to post another post right after I hit submit note. It won't post it and it will fill the url eg: " [http://app.com/ticket/1?body=](http://app.com/ticket/1?body=)blahblah&?method=add ". If I want to post again I have to refresh the page, What am I doing wrong?

Here's my JS.

$('#noteform').submit(function(e) {
e.preventDefault();
var csrf_token = "{{ csrf_token() }}";
var ticketid = "{{ ticket.id }}";
var twoSecMin = $.Deferred();

$.ajax({
url: '/repair/addNote',
dataType: 'json',
type: 'post',
contentType: 'application/json',
data: JSON.stringify( {
"method": 'add',
"body": $('#body').val(),
"ticketid": ticketid,
"private_note": $('#private_note').prop('checked')
}),
processData: true,
success: function( data, textStatus, jQxhr ){
$('#notes').load(' #notes');
$('#notecount').load(' #notecount');
$('input[type=text], textarea').val('');
$('input[type=checkbox]').prop("checked", false);
},
error: function( jqXhr, textStatus, errorThrown ){
console.log( errorThrown );
alert('Error Posting Note!');
}
});


$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!/^(GET|HEAD|OPTIONS|TRACE)$/i.test(settings.type) && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken", csrf_token);
}
}
});

});

and this is my controller logic.

@ticket.route('/addNote', methods=["POST"])
def addNote():
json = request.get_json()
method = json['method']

if method == 'add':
body = json['body']
private = json['private_note']
tid = json['ticketid']

# Create note!
newnote = Notes(
created_by = 0,
is_private = private,
ticket_id = tid,
body = body,
)

db.session.add(newnote)
db.session.commit()

elif method == 'delete':
noteid = json['noteid']

# Delete note.
notetodelete = Notes.query.filter_by(id=noteid).first_or_404()

db.session.delete(notetodelete)
db.session.commit()

elif method == 'update':
return None

return jsonify(result='done')

/r/flask
https://redd.it/8is6qf