AJAX没有在FLASK中发出POST请求

时间:2017-12-20 05:03:24

标签: javascript jquery python ajax flask

倾注了SO以及我能找到的每个博客和/或视频。我是JS / AJAX / JQUERY的新手,并且最近尝试将这些人整合到我的Flask应用程序中,这让我目前感到非常沮丧。尝试通过AJAX调用简单地将表单数据提交到MySQL DB,而无需刷新页面。这是我已经得到的。甚至没有看到检查员提出的要求。也许它没有上路?这是我的代码:

表单HTML

<form name="race_form" id="race_form">
    <input type="text" id="characterID" value="{{ character.id }}" />
    <input type="text" id="raceID" value="{{ attribute.id }}" />
    <button type="submit" id="toggleoption-{{ attribute.attribute_name }}"
    class="btn btn-default char_gen_options_btn" style="margin: 10px 0px 0px 10px;
    background:url(/static/img/blacksmith.jpg) right center no-repeat; background-size:contain;
    background-color:#B5B6AF;font-weight:900;">Select {{ attribute.attribute_name }}</button>
</form>

AJAX

$(document).ready(function() {

$('#race_form').submit(function(event) {

    event.preventDefault();

    var character_id = $('#characterID').val();
    var attribute_id = $('#raceID').val();

    $.ajax({
        url: "{{ url_for('auth.register_race') }}",
        type: 'POST',
        data: { character_id: character_id, attribute_id: attribute_id }
    });

  });
});

Flask View(提交多对多表)

@auth.route('/register_race', methods=['POST'])
@login_required
def register_race():
    character = Character.query.filter_by(id=request.form['characterID']).first()
    character.character_attributes.character_id = request.form['characterID']
    character.character_attributes.attribute_id = request.form['raceID']
    db.session.commit()
    return jsonify({'result': 'success'})

视图引用的models.py中的表:

class Character(db.Model):
    """
    Create a Player Character table - all characters assigned to user
    """
    __tablename__ = 'characters'

    id = db.Column(db.Integer, primary_key=True, unique=True)
    character_name = db.Column(db.String(60), index=True, unique=True)
    experience_points = db.Column(db.Integer)  # Refers to proprietary character build points
    character_lives = db.Column(db.Integer, default=0)
    character_img = db.Column(db.String(300)) # Path to character img on server
    create_date = db.Column(db.DateTime, default=func.now())
    last_update = db.Column(db.DateTime, onupdate=func.now())
    # Keys
    users = db.relationship('User', secondary='user_character')
    attribute = db.relationship('Attribute', secondary='character_attributes')
    items = db.relationship('Items', secondary='inventory')
    files = db.relationship('File', secondary="character_files")

    def __repr__(self):
        return '<Character Name: {}>'.format(self.character_name)


class CharacterAttributes(db.Model):
    """
    Many to many table for characters and assigned attributes
    """
    __tablename__ = 'character_attributes'

    id = db.Column(db.Integer, primary_key=True, unique=True)
    create_date = db.Column(db.DateTime, default=func.now())
    last_update = db.Column(db.DateTime, onupdate=func.now())
    amount_purchased = db.Column(db.Integer)
    # Keys
    character_id = db.Column(db.Integer, db.ForeignKey('characters.id'))
    attribute_id = db.Column(db.Integer, db.ForeignKey('attributes.id'))
    character = db.relationship('Character', backref=backref('character_attributes', cascade='all, delete-orphan'))
    attribute = db.relationship('Attribute', backref=backref('character_attributes', cascade='all, delete-orphan'))

0 个答案:

没有答案