D
D
Dmytro Panyontko2017-03-09 13:10:32
Node.js
Dmytro Panyontko, 2017-03-09 13:10:32

Can't bypass cors?

such a situation, I can’t get around cors, frontend on embe.js backend node.js, put sho on the backend everyone can access but still
d96615cc87244416b56449df06b66731.png
Node.js does not work

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.use(function (req, res, next) {
  res.header("Access-Control-Allow-Origin", req.headers.origin);
  res.header('Access-Control-Allow-Credentials', 'true');
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});

Ajax


$.ajax({
          type: "POST",
          url: "https://polar-headland-32309.herokuapp.com/get-staff",
          async:false,
          dataType: 'json',
          contentType: 'application/json',
          processData: false,
          xhrFields: {
            withCredentials: true
          },
          success:function(data,st,jq) {
            ret=data;
            console.log(ret);
            return ;
          }
        });

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Anton, 2017-03-09
@dmutyo

For CORS and origin * - only get requests via jsonp.
For simple AJAX (if you want to use POST and so on), you need to register a specific origin, either from the list, or (for everyone, not safe) something like this:

app.use(function (req, res, next) {
  res.header("Access-Control-Allow-Origin", req.headers.origin);
  res.header('Access-Control-Allow-Credentials', 'true');
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});
for node.js + express
and specify the withCredentials header for cookies to work:
var ajaxSettings = {
      method: form.method,
      url: form.action,
      data: JSON.stringify(data),
      dataType: 'json',
      contentType: 'application/json',
      processData: false,
      xhrFields: {
        withCredentials: true
      }
    };
    var ajaxRequest = $.ajax(ajaxSettings);
...
for jQuery

S
Stanislav Romanov, 2017-03-09
@Kaer_Morchen

Lay out the request-response log with headers and what it writes to the console

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question