Javascript 意外标记“case”(switch语句)

Javascript 意外标记“case”(switch语句),javascript,switch-statement,Javascript,Switch Statement,我正在制作一个热点,它使用Ajax将用户登录到路由器 如果连接到网络时出现问题,它应该会弹出一个警报,说明存在问题,请联系接待处 我使用的是switch语句,它根据客户端语言/所选语言显示消息 我在代码的其余部分有一堆switch语句,它们都工作得很好,但是这里我得到一个错误,那就是 Uncough SyntaxError:意外标记“case” 任何帮助和解释为什么这个错误只发生在这里将是非常感谢的。多谢各位 function Ajax1 (method, url){ return ne

我正在制作一个热点,它使用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");
    });
}
switch是一个语句,而不是一个表达式。它不能用于对象文字或函数的参数。你需要把它从拒绝论点中去掉

            }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();
            }

错误发生在哪一行?通常这意味着前一行有错误。不能将switch语句放在对象文本中。为什么要尝试在参数中使用switch语句来拒绝?