【问题标题】:Converting this curl to an angular $http.post request将此 curl 转换为有角度的 $http.post 请求
【发布时间】:2016-08-11 08:25:33
【问题描述】:

将这个调用内部 api 函数的 curl 命令转换为 $http.post 请求时遇到了一些问题

curl -X POST 'https://api-dev.message360.com/api/v2/webrtc/authenticate.json' -u 'xxxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxxx' -d 'userName=test@test.com'

----编辑------

我是这样设置的 有一个带有 ng-model=user 的表单

var data = {
 accountSid : "xxxxxx-xxxx-x-xx",
 authToken : "xxxxxxxxxxxxxx",
 userName : "xxxxx@xxxx.com"
}

$http.post(url, data).then(function(response) {
console.log(response);
}

但我收到一个错误,它没有返回任何东西..

【问题讨论】:

标签: angularjs curl


【解决方案1】:

为了帮助调试 $http.post 调用,您需要在发生错误时添加回调。根据您的代码,这是一个示例:

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope, $http) {
    $scope.name = 'World';

    var url = 'https://api-dev.message360.com/api/v2/webrtc/authenticate.json';

    var data = {
        accountSid: "xxxxxx-xxxx-x-xx",
        authToken: "xxxxxxxxxxxxxx",
        userName: "xxxxx@xxxx.com"
    }

    $http.post(url, data, {
        'timeout': 10000
    }).then(function(response) {
        console.log(response);
    },
    function(reason) {
        console.error('ERROR: ' + JSON.stringify(reason));
    });
});

由于虚假数据,超时后我得到-1,但请在Javascript控制台中分享您得到的错误(请参阅我添加的函数(原因)以显示错误消息。)

这里是一个笨蛋based on your example.

【讨论】:

    猜你喜欢
    • 2021-07-08
    • 2018-01-30
    • 2016-04-12
    • 2017-10-31
    • 2020-04-28
    • 1970-01-01
    • 2017-08-03
    • 1970-01-01
    • 2018-04-16
    相关资源
    最近更新 更多