DhanaLaxshmi
DhanaLaxshmi

Reputation: 424

Cross-Origin Request Blocked while accessing api in nodejs application

I have a directive in angularjs which makes a service call to the nodejs api but while accessing it i am getting the error has

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8000/search?search=aqqqqq. (Reason: CORS header 'Access-Control-Allow-Origin' missing).

Angular part of the code is in controller

var app = angular.module('myApp', []);

app.controller('myCtrl', function($scope) {
    $scope.locationURL="http://localhost:8000/search";
});

app.directive('autoCompleteDirective',function($http){
    return{
        restrict:'A',
        scope:{
            url:'@'
        },
        link:function(scope,elm,attrs){
            elm.autocomplete({
                source:function(request,response){
                    $http({method:'get',url:scope.url,params:{search:request.term}}).success(function(data){
                        console.log("!!!!!!!!!!!!!!");
                        console.log(data);
                        response(data);
                    })
                },
                minLength:5
            })
        }
    }
}) 

I ui i am using it has

On the nodejs side the code is some thing like below

var express = require('express');
var app = express();
var mongoose = require('mongoose');
var bodyparser = require('body-parser');
var port = process.env.PORT || 8000;
var router = express.Router();
var cors = require('cors');
var Schema = mongoose.Schema;
var router = express.Router();
var exec = require('exec');
app.use(bodyparser.json());
/*app.use(cors);*/

//establish the connection
var db=mongoose.connect('mongodb://localhost/book_publisherSearch');


//define the schema
var authorSchema = new mongoose.Schema(
    {
        authorId : Number,
        Description : String,
        firstName : String
    });

authorSchema.index({ firstName: 'text'});


var Authors= mongoose.model('Authors',authorSchema);

router.route('/search')
    .get(function(req,res){
        console.log(req.query.search);
        Authors.find({ $text : { $search : req.query.search }},function(err,authors){
            console.log("inside search");
            console.log(authors);
            res.send(authors);
        })
    })


app.use(router);
app.listen(port);

but i am not able to the response in the client side please help me out to figure it i am not getting were i am going wrong

I tried installing cors and use it but still same problem

Upvotes: 0

Views: 516

Answers (1)

Sathik Khan
Sathik Khan

Reputation: 439

Are you rendering your html/client file from the same server or from different server?

Render your page from the same URL you will not get this error.

If its from different server you will be getting a cross origin error. Running the app from two different is the requirement, add allow cross origin header in your node.js app.

Hope this helps.

Upvotes: 1

Related Questions