0

I am trying to send some data to a Flask app using json. When I send it I get a GET error in the console

GET http://super.secret.url/csv?callback=jQuery...

Javascript:

$.ajax({
    type: 'POST',
    contentType: "application/json; charset=utf-8",
    url: "http://super.secret.url/csv?callback=?",
    data: JSON.stringify({message: id, condition: "new"}),
    dataType: "json"
});

Flask (python):

@app.route('/csv', methods=['POST'])
@crossdomain(origin='*')
def edit_csv(path):
    ip = request.remote_addr
    sessionId = request.json['message']
    type = request.json['condition']

    csvFile = csv.reader(open('ip_log.csv'))
    csvLines = [l for l in csvFile]



    if(type == "new"):
        for i in range(0, len(csvLines)):
            if(csvLines[i][0] == ip):
                csvLines[i][1] == sessionId
                break


    csvwriter = csv.writer(open('ip_log.csv', 'w'))
    csvwriter.writerows(csvLines)

    return ""

Edit

I am getting a 405. I know this is a cross domain request but I do have the server setup to handle that. I have a different function in the python file that works cross domain.

Josh Young
  • 125
  • 3
  • 15

1 Answers1

0

To solve the cross domain problem, you may try JSONP instead of JSON. For instance, the ajax code gives as follows:

$.ajax({
    type: 'POST',
    dataType: 'jsonp'
    url: "http://super.secret.url/csv?callback=?", 
    jsonp: 'callback'//to get your own callback function name
    jsonpCallback:'youOwnFunction',//'youOwnFunction' is callback function
    //success or error function
});

return data shows like that

youOwnFunction({
//return data
});
lolilukia
  • 11
  • 3
  • Is is possible to make a POST with jsonp. This post says no: http://stackoverflow.com/questions/35477537/send-data-using-post-to-jsonp-request – Josh Young Mar 13 '17 at 02:36
  • High version jQuery will transfer 'POST' to 'GET' to obtain data, if you insist of real 'POST', you can choose other alternate way. – lolilukia Mar 13 '17 at 03:01