Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. fetch('http://localhost:8080/login', {
  2. method: 'POST',
  3. mode:'cors',
  4. headers: {
  5. 'Accept': 'application/json',
  6. 'Content-Type': 'application/json'
  7. },
  8. body: JSON.stringify({
  9. username: this.state.username,
  10. password: this.state.password
  11. })
  12. });
  13.  
  14. var responseHeaders = {
  15. "access-control-allow-origin": "*",
  16. "access-control-allow-methods": "GET, POST, PUT, DELETE, OPTIONS",
  17. "access-control-allow-headers": "content-type, accept",
  18. "access-control-max-age": 10,
  19. "Content-Type": "application/json"
  20. };
  21.  
  22. app.post('/login', function(req, res, next) {
  23. if (req.method == "OPTIONS") {
  24. res.writeHead(statusCode, responseHeaders);
  25. res.end();
  26. }
  27. console.log("hello");
  28. ...
  29.  
  30. OPTIONS /login 200 8.570 ms - 4
  31.  
  32. app.options("*",function(req,res,next){
  33. res.header("Access-Control-Allow-Origin", req.get("Origin")||"*");
  34. res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  35. //other headers here
  36. res.status(200).end();
  37. });
  38.  
  39. app.use(function(req, res, next) {
  40. res.header("Access-Control-Allow-Origin", "*");
  41. res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  42. next();
  43. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement