使用Javascript通过API

时间:2015-10-07 16:07:23

标签: javascript api

我尝试使用this tutorial创建一个Javascript代码,将代码从预制表单发送到Call API,以发送短信和电话。

<script type="text/javascript">
function() {
        var self = this;
        self.tasksURI = 'https://www.callfire.com/api/1.1/rest/';
        self.username = "-api user-";
        self.password = "-api password-";
        self.tasks = ko.observableArray();

        self.ajax = function(uri, method, data) {
            var request = {
                url: uri,
                type: method,
                contentType: "application/json",
                accepts: "application/json",
                cache: false,
                dataType: 'json',
                data: JSON.stringify(data),
                beforeSend: function (xhr) {
                    xhr.setRequestHeader("Authorization", 
                        "Basic " + btoa(self.username + ":" + self.password));
                },
                error: function(jqXHR) {
                    console.log("ajax error " + jqXHR.status);
                }
            };
            return $.ajax(request);
        }
            self.updateTask = function(text, newTask) {
                var i = self.tasks.indexOf(task);
                self.tasks()[i].uri(newTask.uri);
                self.tasks()[i].type(newTask.$(communcationType));
                self.tasks()[i].to(newTask.$(phoneNumber));
                self.tasks()[i].message(newTask.$(broadcastMessage));
                self.tasks()[i].done(newTask.done);
            }
       self.add = function(task) {
                self.ajax(self.tasksURI, 'POST', text).done(function(data) {
                    self.tasks.push({
                        uri: ko.observable(data.task.uri),
                        type: ko.observable(data.task.type),
                        to: ko.observable(data.task.to),
                        message: ko.observable(data.task.message),
                        done: ko.observable(data.task.done)
                    });
                });
            }
        });
</script>

我在标头中插入了API信息以及Knockout和jQuery信息,并尝试运行表单,但表单和网站API之间似乎没有任何通信。我是否将占位符插入错误的位置?

或者有没有人能为我的情况找到更好的解决方案?

0 个答案:

没有答案