2

I have the below Python Flask router:

@app.route('/create', methods=['GET', 'POST'])
@crossdomain(origin='*')
def create():
    if request.method == 'POST':
        print(request.form)
        title = request.form['title']
        url = request.form['url']

        new_mark = Mark(
            title=title,
            url=url
        )
        new_mark.save()

        return new_mark

When I do an ajax call (below) it responds with a 400 error.

$.ajax({
     type: 'POST',
     url: 'http://localhost:5000/create',
     data: {
            'title': sender.title,
            'url': sender.url
           },
     xhrFields: {
            withCredentials: true
           },
     dataType: 'json'
});

When I try printing out request it prints an empty immutableMultiDict. Any idea why it is giving this 400 and why there is no data?

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
chromedude
  • 4,246
  • 16
  • 65
  • 96

2 Answers2

1

Your ajax call is sending json-encoded data. I guess you should decode.

import json

data = json.loads(request.data)

print data.get("title")
Fatih Erikli
  • 2,986
  • 2
  • 15
  • 11
0

I am stupid. I was not actually sending any data because sender.url and sender.title did not contain any values -_-.

chromedude
  • 4,246
  • 16
  • 65
  • 96