I have create rest api in codeigniter and trying to consume it in phonegap app But its giving me Error - 401 Unauthorized
in Phonegap index.html
page.
I am using Token Based Authentication in codeigniter. Rest api is working fine in postman and also its working fine when rest api consumed in Codeigniter View using same ajax code
Codeigniter Rest Server Code I referenced -
https://github.com/chriskacerguis/codeigniter-restserver
Below is Error I'm Receiving in Phonegap's Index.html
-
Phonegap App URL (http://192.168.1.2:3000/)
Below is Code I'm Using on Both Side in phonegap / Codeigniter View 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>
Screenshot of Rest API Working fine in Postman -
Screenshot of COnsole When API Consumed in Codeigniter View using ajax -
Basically, you won't be able to access http://localhost/webapi/api/user/ from your emulator as your XAMPP server is hosted on a different machine (the emulator is a virtual machine). By changing the URL of your request to http://192.168.1.61/webapi/api/user/ for example, you can access the apache server hosted on your PC. You'll have to find out your local IP by opening a command prompt and typing ipconfig. You'll also need to ensure that your networks firewall allows incoming traffic from port 80.
Or you can try on cpanel or plesk server.