jQuery POST:将Content-Type设置为text / plain

时间:2016-10-27 14:29:01

标签: jquery

我无法弄清楚为什么这不起作用。在服务器上,我有这个:

@app.route('/message', methods=['POST'])
def print_post():
    if request.headers['Content-Type'] == 'text/plain':
        logging.warning(request.data)
        return "Text Message: " + request.data
    else:
        logging.warning('didnt work')
        return 'Unsupported Media Type'

我通过浏览器发送此请求:

$.ajax({
    type: "POST",
    url: "https://localhost:8090/message",
    data: 'this is a message'
    contentType: 'test/plain'
})

但我一直收到错误Uncaught SyntaxError: Unexpected identifier

我做错了什么?

1 个答案:

答案 0 :(得分:6)

您在ajax调用中的data属性后面缺少逗号,contentType: 'text/plain'

$.ajax({
    type: "POST",
    url: "https://localhost:8090/message",
    data: 'this is a message', // <-- Put comma here
    contentType: 'text/plain'
})