如何从curl终端翻译成javascript

时间:2016-04-07 23:18:14

标签: javascript jquery curl terminal

我需要将数据发送给卡处理公司,但公司api代码示例使用curl终端。

这是api接受的代码格式

curl -X POST -u "login:password" -H "Accept: application/json" -H "X-PJ-Application-Key: YOUR_PRODUCTION_APP_KEY" \
-d "action=CHARGE" \
-d "cardTrack=%B4444333322221111^First/Last^1712980100000?;4444333322221111=1712980100000?" \
-d "amountBase=1.00" \
"https://api.payjunction.com/transactions"

现在我想知道javascript术语的意思。

1 个答案:

答案 0 :(得分:2)

或多或少:

$.ajax({
    url: "https://api.payjunction.com/transactions",
    type: "POST",
    data: {
        action: "CHARGE",
        cardTrack: "%B4444333322221111^First/Last^1712980100000?;4444333322221111=1712980100000?",
        amountBase: 1.00
    },
    headers: {
        "Accept": "application/json",
        "X-PJ-Application-Key": "YOUR_PRODUCTION_APP_KEY"
    },
    beforeSend: function (xhr) {
        xhr.setRequestHeader ("Authorization", "Basic " + btoa(username + ":" + password));
    },
    dataType: 'json'
});