Как отправить POST запрос с Vue.js на PHP?

Здравствуйте. Помогите, пожалуйста, разобраться, почему я не могу отправить POST-запрос через Vue.js, или не могу правильно его принять через PHP.
Вот коды Vue.js. Отправление:
let tests = {
    cat1: {
        name: 'Авторизация',
        items: {
            authorize1: {
                name: 'Авторизация',
                subname: '',
                request: {
                    method: 'POST',
                    link: 'auth',
                    data: {
                        login: 'admin',
                        password: 'password'
                    }
                },
                test: (result, status) => {
                    if (status.status !== 404 && status.status != 403 && status.status != 500) {
                        return true;
                    }

                    return false;
                }
            }}}}


Метод:
let f = (method, link, data, cb) => {
    let options = {
        method: method,
        headers: new Headers()
    };

    if(data instanceof FormData) {
        options.body = data;
    } else if(data) {
        let fd = new FormData();
        for(let key in data) fd.append(key, data[key]);
        options.body = fd;
    }
    if(vue.bearerEnabled)
        options.headers.append('Authorization', 'Bearer ' + vue.bearer);

    let status = false;
    fetch(link, options)
        .then(r => {
            if(r.status == 404) {
                cb(null, r);
            }

            status = r;
            return r.json();
        }).then(r => {
            cb(r, status);
        });
};
let vue;

window.onload = _ => {
    vue = new Vue({
        el: '#app',
        data: {
            url: 'http://wsr.cc:898/osnov1/api/',
            isStart: false,
            tests: [],
            bearer: '',
            bearerEnabled: false
        },

        methods: {
            appStart() {
                if (this.url !== '') {
                    this.isStart = true;
                    let newTests = {};

                    for(cat in tests) {
                        newTests[cat] = {};
                        newTests[cat].name = tests[cat].name;
                        newTests[cat].items = {};

                        for(id in tests[cat].items) {
                            newTests[cat].items[id] = tests[cat].items[id];
                            newTests[cat].items[id].sended = false;
                            newTests[cat].items[id].status = null;
                            newTests[cat].items[id].response = {
                                statusCode: null,
                                statusText: null,
                                body: null
                            };

                            newTests[cat].items[id].ok = false;
                        }
                    }

                    this.tests = newTests
                }
            },

            test(cat, id) {
                let item = this.tests[cat].items[id];
                let data = item.custom ? new FormData(document.querySelector('[data-form=' + cat + '_' + id + ']')) : item.request.data;
                let url = this.url + item.request.link;

                if(item.request.customLink) {
                    for(let key in item.request.customLink) {
                        url = url.replace('{' + key + '}', document.querySelector('[data-value="' + cat + '_' + id + '_' + key + '"]').value);
                    }
                }

                f(item.request.method, url, data, (result, status) => {
                    this.tests[cat].items[id].status = item.test(result, status);

                    this.tests[cat].items[id].sended = true;
                    this.tests[cat].items[id].originalUrl = url;
                    this.tests[cat].items[id].response.statusCode = status.status;
                    this.tests[cat].items[id].response.statusText = status.statusText;

                    this.tests[cat].items[id].response.body = JSON.stringify(result, null, 4);
                });
            },

        }
    });
};


В самом PHP в POST ничего нет, "file_get_contents('php://input')" так же не помогает, там пусто.
  • Вопрос задан
  • 3775 просмотров
Пригласить эксперта
Ответы на вопрос 3
Konstantin18ko
@Konstantin18ko
Стоматолог
Ajax, Axios, Vue resource - вот то что отправляет post запросы.
Ответ написан
@Alex_Zdorgor
Использовать ajax запрос из любой библиотеки или чистого js. Но для Vue чаще всего используют библиотеку Axios.
https://ru.vuejs.org/v2/cookbook/using-axios-to-co...
Ответ написан
Комментировать
@Viktor_Dav
Не совсем понятно, что это такое. Вы не могли бы привести полный код?
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы