phonegap / cordova http basic auth

时间:2016-07-12 15:07:00

标签: javascript ajax cordova html-framework-7

如何在phonegap ap中通过http身份验证从Web服务器获取数据? 我试过(使用Dom 7):



$$.ajax({
  url: 'http://@api.mysite.com/api/json',
  method: 'GET',
  dataType: 'json',
  xhrFields: { withCredentials: true },
  beforeSend: function (xhr){
    xhr.setRequestHeader('Authorization', 'Basic ' + btoa(username + ":" + password));
  },
  success: function (data, status, xhr) {
    console.log(arguments);
  },
  error: function (xhr, status) {
    console.log(arguments);
  }
})






$$.ajax({
  url: 'http://' + username + ':' + password + '@api.mysite.com/api/json',
  method: 'GET',
  dataType: 'json',
  xhrFields: { withCredentials: true },
  success: function (data, status, xhr) {
    console.log(arguments);
  },
  error: function (xhr, status) {
    console.log(arguments);
  }
})




但我回复了statusCode = 401。

0 个答案:

没有答案