[英]Phonegap Error : 401 Unauthorized - consuming Rest API with Basic Auth in Phonegap
我已經在codeigniter中創建了rest api並嘗試在phonegap app中使用它但它在Phonegap index.html
頁面中給了我錯誤 - 401 Unauthorized
。
我在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/ )
下面是我在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>
基本上,您將無法從模擬器訪問http:// localhost / webapi / api / user / ,因為您的XAMPP服務器托管在不同的計算機上(模擬器是虛擬機)。 例如,通過將請求的URL更改為http://192.168.1.61/webapi/api/user/ ,您可以訪問PC上托管的apache服務器。 您必須通過打開命令提示符並鍵入ipconfig來找到本地IP。 您還需要確保您的網絡防火牆允許來自端口80的傳入流量。
或者你可以試試cpanel或plesk服務器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.