I need to develop a hybrid app for my website using ionic for UI and i am using node js for APi creation. The problem is am able to access the API through the browser if i call the same API from my angularjs i am not able to retrieve the response. this is the code at node js side:
var app = require('express')();
var http = require('http').Server(app);
var mysql = require('mysql');
var bodyParser = require("body-parser");
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'root',
database : 'student',
});
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.get('/stud',function(req,res){
connection.query("SELECT * from stu_details",function(err, rows, fields){
if (err) throw err;
res.contentType('application/json');
console.log("entered");
res.send(JSON.stringify(rows));
res.end();
});
});
code at angularjs:
var app1 = angular.module('starter', ['ionic']);
app1.controller('mycontrl',function($scope,$http){
$http.get("http://127.0.0.1:3000/stud")
.then(function(response) {
$scope.names = response.body;
})
});
The thing is that at the angular side it is not entering into the "then" function; i.e., the response is not succeeding. But I am able to get the body part if I call from my browser.