如何为curl命令编写$ .ajax等效项?

时间:2015-03-26 06:17:55

标签: javascript jquery ajax json curl

卷曲命令:

curl -u "username:password" -X POST "https://api-ssl.bitly.com/oauth/access_token"

我尝试过:

$.ajax({
        url: 'https://api-ssl.bitly.com/oauth/access_token',

        beforeSend: function (xhr) {
            xhr.setRequestHeader("Authorization", "Basic " + btoa("username:password"));
        },
        type: "POST",
        dataType: 'json',
        contentType: 'application/x-www-form-urlencoded',      

        success: function (result) {
            console.log(result);
        },
        error: function () {

            alert("Cannot get data");
        }
    });

我尝试过使用dataType:jsonp,contentType:json和其他方法,但总是得到错误响应?我做错了什么?

0 个答案:

没有答案