Неожиданный токен 'case' (оператор switch) - PullRequest
0 голосов
/ 12 марта 2020

Я создаю точку доступа, которая использует Ajax для входа пользователя в маршрутизатор.

Если есть проблема с подключением к сети, должно появиться предупреждение, которое говорит: Возникла проблема. Пожалуйста, свяжитесь с приемной.

Я использую оператор switch, который отображает сообщение в зависимости от языка клиента / выбранного языка.

У меня есть куча операторов switch в остальной части моего кода, и все они работают нормально, но здесь я получаю сообщение об ошибке ...

Uncough SyntaxError: Неожиданный токен 'case'

Любая помощь и объяснение того, почему эта ошибка возникает только здесь, будет очень цениться. Спасибо

function Ajax1 (method, url){
    return new Promise (function (resolve,  reject){
        let xhr = new XMLHttpRequest();
        xhr.open('POST', 'http://10.5.50.1/login', true);
        xhr.onload = function(){
            if(this.status >= 200 && this.status < 300){
                resolve(xhr.response);
            }else{
                reject({
                //  console.log("XHR1 " + this.readyState);
                //  console.log("XHR1 " + this.status);
                    switch (global){
                        case "sl":
                            alert("Prišlo je do napake. Prosim obrnite se na recepcijo.");
                            break;
                        case "en":
                            alert("There seems to be an issues. Please contact the reception.");
                            break;
                        case "de":
                            alert("There seems to be an issues. Please contact the reception.");
                            break;
                        case "it":
                            alert("There seems to be an issues. Please contact the reception.");
                            break;
                        case "hr":
                            alert("There seems to be an issues. Please contact the reception.");
                            break;
                        case "ru":
                            alert("There seems to be an issues. Please contact the reception.");
                            break;
                        default:
                            alert("There seems to be an issues. Please contact the reception.");
                    }
                });
            }
        };
        xhr.onerror = function (){
            reject({
            //  console.log("XHR1 " + this.readyState);
            //  console.log("XHR1 " + this.status);
                switch (global){
                    case "sl":
                        alert("Prišlo je do napake. Prosim obrnite se na recepcijo.");
                        break;
                    case "en":
                        alert("There seems to be an issues. Please contact the reception.");
                        break;
                    case "de":
                        alert("There seems to be an issues. Please contact the reception.");
                        break;
                    case "it":
                        alert("There seems to be an issues. Please contact the reception.");
                        break;
                    case "hr":
                        alert("There seems to be an issues. Please contact the reception.");
                        break;
                    case "ru":
                        alert("There seems to be an issues. Please contact the reception.");
                        break;
                    default:
                        alert("There seems to be an issues. Please contact the reception.");
                }
            });
        };

        console.log("sent");
        xhr.send("username=HSuser&password=SimpleUserPassword");
    });
}

1 Ответ

1 голос
/ 12 марта 2020

switch - это утверждение, а не выражение. Его нельзя использовать в литерале объекта или в аргументе функции. Вы должны исключить его из аргумента reject().

            }else{
                switch (global){
                    case "sl":
                        alert("Prišlo je do napake. Prosim obrnite se na recepcijo.");
                        break;
                    case "en":
                        alert("There seems to be an issues. Please contact the reception.");
                        break;
                    case "de":
                        alert("There seems to be an issues. Please contact the reception.");
                        break;
                    case "it":
                        alert("There seems to be an issues. Please contact the reception.");
                        break;
                    case "hr":
                        alert("There seems to be an issues. Please contact the reception.");
                        break;
                    case "ru":
                        alert("There seems to be an issues. Please contact the reception.");
                        break;
                    default:
                        alert("There seems to be an issues. Please contact the reception.");
                }
                reject();
            }
...