Ajax POST未到达服务器,仅在Firefox中返回400

时间:2019-05-29 20:59:46

标签: javascript jquery ajax apache firefox

我有一个在Apache中运行的Flask服务器,该服务器返回带有按钮的HTML页面,以将AJAX POST请求提交到相对URL。在Firefox中,AJAX调用立即失败,并显示400,记录为POST response ERROR code: 400。将xhr对象记录到控制台将生成<unavailable>,其responseText属性为undefined。无论将什么URL传递给AJAX,响应都是完全相同的。绝对URL(http://thehostname.com/path/send-email)也给出相同的结果。

Apache的access.log显示在AJAX请求时没有请求传入。在开发工具的Firefox的“网络”标签中根本没有提出该请求。在所有其他条件相同的情况下,该请求在Chrome浏览器中均能正常运行,对于cURL也是如此。

代码在下面。请帮忙!

HTML:

<img width="20px" src="mail.png" alt="" onerror="this.style.display='none'" onclick="sendEmail('val1', 'val2', 'val3')">

JS:

    function sendEmail(val1, val2, val3) {
        val1 = prompt('Prompt2 for ' + val2 + ':', val1);
        console.log('Val1: '+ val1);
        if(val1) {
            let xhr = $.ajax({
                method: 'POST',
                url: 'send-email',
                // Tried with and without stringify
                data: JSON.stringify({param1: val1, param2: val2, param3: val3}),
                contentType: "application/json; charset=utf-8",
                dataType: 'text',
                cache: false,
                traditional: true
            }).done(function(){
                alert("Sent!");
            }).fail(function(data){
                console.log("Info:");
                console.log(xhr.responseText);
                alert('Failed to send');
            });
        }
        console.log("Ret false");
        return false;
    };

Python端点:

@app.route('/autorack_testing_report/send-email', methods=['POST'])
@app.route('/send-email', methods=['POST'])
def send_email():
    print("Send email") # Not getting printed
    info = request.form or request.json
    if not (info and info.get('param1') and info.get('param2')):
        abort(400, 'Must supply recipient and ticket')
    send_to_list = re.split(',|;', info['param1'])
    send_to = []
    for s in send_to_list:
        if s.strip():
            send_to.append(s.strip() + '@domain.com')
    send_to = ','.join(send_to)
    ticket = info['ticket']
    smtp = smtplib.SMTP('mysmtphost', 25)
    msg = MIMEMultipart('alternative')
    msg['Subject'] = 'subject'
    msg['From'] = msg['To'] = send_to
    msg.attach(MIMEText('somecontent', 'plain'))
    smtp.sendmail(send_to, send_to, msg.as_string())
    smtp.quit()
    resp = make_response('Success', 200)
    resp.headers['Content-Type'] = 'text/plain'
    return resp

Firefox控制台输出:

Val1: myinput
Ret false
POST response ERROR code: 400
POST response ERROR code: 400
Info:
undefined

没有Python输出,因为Apache没有收到请求。

1 个答案:

答案 0 :(得分:0)

400错误请求响应状态代码表示由于某些原因(例如格式错误的请求语法,无效的请求消息框架或欺骗性的请求路由),服务器无法或将不会处理请求。 “发送电子邮件”指向何处?