A
A
Alexey Bespalov2019-05-06 12:22:13
JavaScript
Alexey Bespalov, 2019-05-06 12:22:13

How to allow CORS requests in 1C?

I'm trying to execute ajax requests from JS in 1C. Already tried everything, still errors.
5ccffb03ed6e1946105526.png

xhr.js?47bc:178 OPTIONS http://localhost/bespalov_trade/hs/api/GetInfoByTel?tel=9999999999 401 (Unauthorized)
localhost/:1 Access to XMLHttpRequest at 'http://localhost/bespalov_trade/hs/api/GetInfoByTel?tel=9999999999' from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

5ccffb0d7ce6a585641308.png
axios({
        method: "get",
        url:
          "http://localhost/bespalov_trade/hs/api/GetInfoByTel?tel=9999999999",
        auth: {
          username: "web",
          password: "123"
        }
      })
        .then(function(response) {
          console.log(response);
        })
        .catch(function(error) {
          console.log(error);
        });

A piece when from 1C, where I set the headers
Ответ = Новый HTTPСервисОтвет(200);
  Ответ.Заголовки.Вставить("Content-Type", "charset=utf-8");
  Ответ.Заголовки.Вставить("Content-Type", "text/html");
  Ответ.Заголовки.Вставить("Access-Control-Allow-Origin", ""); 
  Ответ.Заголовки.Вставить("Access-Control-Allow-Headers", "*");

apache server. Platform 3.8.14 Tell me
, for sure, some kind of trifle spoils everything.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Rsa97, 2019-05-06
@FreeArcher

CORS has nothing to do with it, its errors look different .
You have an authorization error.

E
Evgeny Koryakin, 2019-05-06
@zettend

In order to avoid such problems, I use Nginx instead of Apache. And in general, in working with the API, it is more suitable, as practice shows.

H
Horus123, 2020-11-06
@Horus123

Most likely it is no longer relevant to you, but others may come in handy. Most likely on 1s basic authorization.
For authorization, you need to pass login:password in base64.

const token = Buffer.from(`${email}:${password}`, "utf8").toString(
            "base64"
        );
     const config = {
            method: "get",
            url:
                "http://example.com",
            headers: {
                Authorization: `Basic ${token}`,
            }
        }
        axios(config)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question