从客户端向后端发送json时出现问题

时间:2020-09-20 19:59:20

标签: javascript python json flask

我正在尝试将Json帖子发送到Flask后端。

javascript客户端代码:

    submit.addEventListener('click', function(){
    var myHeaders = new Headers();
    myHeaders.append("Content-Type", "application/json");


    var raw = JSON.stringify({"amount":Number(amount.value),
                              "ccnumber":`${ccnumber.value}`,
                              "month":`${ccmonth.value}`,
                              "year":`${ccyear.value}`,
                              "zip":`${zip.value}`,
                              "cvv":`${cvv.value}`,
                              "first_name":`${name.value}`,
                              "last_name":`${lastname.value}`,
                              "address1":`${address.value}`,
                              "city":`${city.value}`,
                              "state":`${state.value}`,
                              "country":`${country.value}`,
                              "isJava": "true",
                              "lang": navigator.language,
                              "colorDepth": screen.colorDepth,
                              "browserTZ": new Date().getTimezoneOffset(),
                              "screenWidth": screen.width,
                              "screenHeight": screen.height,
                              "browserAgent": navigator.userAgent});

    var requestOptions = {
        method: 'POST',
        headers: myHeaders,
        body: raw,
        redirect: 'follow' };

        fetch("http://0.0.0.0:80/consulting-h", requestOptions)
        .then(response => response.text())
            .then(result => console.log(result))
            .catch(error => console.log('error', error));
})

后端代码:

requestJson = flask.request.json

    amount = requestJson['amount']
    ccnumber = requestJson['ccnumber']
    month = requestJson['month']
    year = requestJson['year']
    ccexp = f'{month}{year}'
    zip = requestJson['zip']
    cvv = requestJson['cvv']
    first_name = requestJson['first_name']
    last_name = requestJson['last_name']
    address1 = requestJson['address1']
    city = requestJson['city']
    state = requestJson['state']
    country = requestJson['country']

    # Browser Fingerprint
    isJava = requestJson['isJava']
    lang = requestJson['lang']
    colorDepth = requestJson['colorDepth']
    browserTZ = requestJson['browserTZ']
    screenWidth = requestJson['screenWidth']
    screenHeight = requestJson['screenHeight']
    browserAgent = requestJson['browserAgent']


    dataDict = {
        "challengeIndicator": "01",
        "amount": amount,
        "pan": ccnumber,
        "month": month,
        "year": year,
        "browser": {
            "browserAcceptHeader": "application/json",
            "browserJavaEnabled": isJava,
            "browserLanguage": lang,
            "browserColorDepth": colorDepth,
            "browserTZ": browserTZ,
            "browserScreenWidth": screenWidth,
            "browserScreenHeight": screenHeight,
            "browserUserAgent": browserAgent
        },
        "threeDSRequestorURL": requestorURL,

    }

    data = json.dumps(dataDict)


    print(data)



    postResponse =  requests.post('https://apisandbox.3dsintegrator.com/v2/authenticate/browser', headers=headers(),
                                 data=data).json()

    print(postResponse)

结果:

{"challengeIndicator": "01", "amount": 1, "pan": "5107747733373841", "month": "06", "year":      "20", "browser": {"browserAcceptHeader": "application/json", "browserJavaEnabled": "true", "browserLanguage": "en", "browserColorDepth": "45", "browserTZ": "245", "browserScreenWidth": "1440", "browserScreenHeight": "900", "browserUserAgent": "Mozilla/5.0 (iPhone; CPU iPhone OS 12_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Mobile/15E148 Safari/604.1"}, "threeDSRequestorURL": "http://5090ca5ec085.ngrok.io"}

服务器响应:

{'错误':'无法解析请求正文。请检查JSON格式'}

如果对Postman进行了相同的请求,它将返回有效。

Json与PostmanL一起发送

var raw =    JSON.stringify({"amount":1,"ccnumber":"5107747722273841","month":"09","yea r":"20","zip":"33132","cvv":"352","first_name":"kevin","last_name":"luciano","address1":"244 biscayne","city":"Miami","state":"FL","country":"US","isJava":true,"lang":"en","colorDepth":"45","browserTZ":"230","screenWidth":"1440","screenHeight":"900","browserAgent":"Mozilla/5.0 (iPhone; CPU iPhone OS 12_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Mobile/15E148 Safari/604.1"});

从客户端发送请求时,我该怎么做才能使工作正常?

1 个答案:

答案 0 :(得分:0)

尝试类似JSON.stringify({amount:1, ccnumber: "5107747722273841"的方法 请注意,参数名称周围缺少“。

附带说明,在页面上记录信用卡详细信息,使用Paypal,eWay,payway,stripe等付款网关不是一个好主意。