Plivo获取所有不支持jquery get请求的实时调用

时间:2017-09-19 07:42:23

标签: jquery plivo

我用它来调用ajax get request

       // Assign handlers immediately after making the request,
       // and remember the jqxhr object for this request
       var jqxhr = $.get("https://api.plivo.com/v1/Account/{auth_id}/Call/?status=live", function(data) {
            console.log( "success" + data);
       })

       .done(function(data) {
           console.log( "second success" );
       })

       .fail(function(data) {
           console.log( "error" );
       })
       .always(function(data) {
           console.log( "finished" );
           console.log(data);
       });

        // Perform other work here ...

        // Set another completion function for the request above
        jqxhr.always(function() {
            alert( "second finished" );
        });
    })

并且回复错误,请参阅此处https://www.screencast.com/t/lI6UdQ5H

注意: {auth_id}我已经用plivo提供的auth_id替换了它。

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您的代码似乎有效。我在这里做了一个演示。请检查你的ajax网址。

https://jsfiddle.net/aquadk/ez8Lae5v/3/

  function mylogger(aValue) {
    var theDiv = document.getElementById("msg");
    theDiv.innerHTML += aValue + '<br>'
      //console.log( "second success" );
  }
  var USERNAME = 'userid';
  var PASSWORD = 'pass'
    // Assign handlers immediately after making the request,
    // and remember the jqxhr object for this request
  var jqxhr = $.ajax({
    type: "GET",
    url: "https://api.plivo.com/v1/Account/{auth_id}/Call/?status=live",
    dataType: 'json',
    async: false,
    headers: {
      "Authorization": "Basic " + btoa(USERNAME + ":" + PASSWORD)
    },
    success: function() {
      mylogger("success" + data);
    }
  })

  .done(function(data) {
    mylogger("second success");
  })

  .fail(function(data) {
      mylogger("error");
    })
    .always(function(data) {
      mylogger("finished");
      mylogger(data);
    });

  // Perform other work here ...

  // Set another completion function for the request above
  jqxhr.always(function() {
    mylogger("second finished");
  });

HTML

<Div id="msg">

</Div>