Phonegap错误:401未经授权 - 在Phonegap中使用基本身份验证的Rest API

问题描述 投票:0回答:1

我已经在codeigniter中创建了rest api并尝试在phonegap应用程序中使用它但它在Phonegap 401 Unauthorized页面中给了我错误 - index.html

我在codeigniter中使用基于令牌的身份验证。 Rest api在邮递员工作正常,并且当使用相同的ajax代码在Codeigniter View中使用rest api时工作正常

Codeigniter Rest服务器代码我引用 -

https://github.com/chriskacerguis/codeigniter-restserver

下面是我在Phonegap的Index.html收到的错误 -

Phonegap App URL(http://192.168.1.2:3000/

enter image description here

enter image description here

下面是我在phonegap / Codeigniter查看URL(http://localhost/webapi/welcome/index)中双面使用的代码

   <script
  src="http://code.jquery.com/jquery-3.3.1.min.js"
  integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
  crossorigin="anonymous"></script>
     <script>
    var settings = {
  "async": true,
  "crossDomain": true,
  "url": "http://localhost/webapi/api/user/",
  "method": "GET",
  "headers": {
    "X-API-KEY": "7c4a8d09ca3762af61e59520943dc26494f8941b",
    "Authorization": "Basic YWRtaW46MTIzNA==",
    "cache-control": "no-cache",
    "Postman-Token": "552d3078-6d54-4d59-ba79-c94248b154b3"
  }
}
console.log("settings are -");
         console.log(settings);
$.ajax(settings).done(function (response) {
    console.log("here api result come - ");
  console.log(response);
});
    </script>

Rest API在Postman中工作正常的屏幕截图 - enter image description here

使用ajax - enter image description here在Codeigniter视图中使用API​​时的COnsole的屏幕截图

php rest phonegap codeigniter-restserver codeigniter-restapi
1个回答
0
投票

基本上,您将无法从模拟器访问http://localhost/webapi/api/user/,因为您的XAMPP服务器托管在不同的计算机上(模拟器是虚拟机)。例如,通过将请求的URL更改为http://192.168.1.61/webapi/api/user/,您可以访问PC上托管的apache服务器。您必须通过打开命令提示符并键入ipconfig来找到本地IP。您还需要确保您的网络防火墙允许来自端口80的传入流量。

或者你可以试试cpanel或plesk服务器。

© www.soinside.com 2019 - 2024. All rights reserved.