0

Actually i need to update to database using node js update method, but i have tried lot of ways but since its not working.

This are all i have tried..

Angular 2

 private getHeaders(){
        let headers = new Headers();
        headers.append('Accept', 'application/json');
        return headers;
    }

 updateMethod(params : any,url : string){           
    //var content = new URLSearchParams();
    // let searchParams = new URLSearchParams();
        params.url = url;
        /*  for (let param in params) {
               searchParams.set(param, params[param]);
        }*/          
        /*let apiHeaders = new Headers({  'Accept' : 'application/json','Content-Type': 'application/json',"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS" });
          apiHeaders.append('Method', 'PUT');                           
          apiHeaders.append('Content-Type', 'application/json;charset=UTF-8');
          apiHeaders.append('Access-Control-Allow-Credentials',"true");*/
          //console.log(searchParams);          
          return this.http
             .put(this.baseUrl+"api/update", JSON.stringify(params), {headers: this.getHeaders()})
             .map(res => res.json());
    }   

Nodejs

//Update Method
router.put('/api/update', function(req, res) {
    res.setHeader('Access-Control-Allow-Origin', '*');  

    var params = {};        
    for(var keyName in req.body){
        if(keyName != "url"){
            params[keyName] = req.body[keyName];    
        }
    }   
    console.log(params);

    //Update Api Call
    request.put(
        baseUrl + req.body.url,
        { json: params },
        function (error, response, body) {
            if (!error && response.statusCode == 200) {
                res.send(body);
            }else{  
                if(error == null){
                    res.send(false);    
                }else{
                    res.send(error);
                }
            }
        }
     );

}); 

I'm getting this error every time:

XMLHttpRequest cannot load http://localhost:8081/api/update. Method PUT is not allowed by Access-Control-Allow-Methods in preflight response.

Please give any solution..

1 Answer 1

1

if you using express, i'm prefer include this lib, to enable CORS

const cors = require('cors');

const app = express();
app.use(cors());

note that: put app.use(cors()); before declare router or in your router file

const cors = require('cors');


router.put('/api/update', cors(), function(req, res) {});

package info here: https://www.npmjs.com/package/cors

Sign up to request clarification or add additional context in comments.

2 Comments

could you post your server js entry point file, ex: index.js, or app.js, etc.
and put app.use(cors()); before define router or router.put('/api/update', cors(), function(req, res) {});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.