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
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