JavaScript语法错误

时间:2015-09-21 01:42:30

标签: javascript syntax

我在JavaScript中有两个函数并收到以下错误:

  参数列表

之后的

Uncaught SyntaxError:missing)

它没有给我一个行号,我似乎无法找到罪魁祸首;我将不胜感激:

function makeRequest() {
    var xmlhttp = new XMLHttpRequest();
    console.log('reached');
    xmlhttp.onreadystatechange = function() //This part is actually executed last
    {
        console.log("within");
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) // Was the request processed successfully?
        {
            console.log(location);
            console.log(xmlhttp.responseText);
            first = xmlhttp.responseText.substring(xmlhttp.responseText
                    .search('first:') + 6, xmlhttp.responseText
                    .search(',last'));
            console.log("First: " + first);
            last = xmlhttp.responseText.substring(xmlhttp.responseText
                    .search(',last') + 6, xmlhttp.responseText
                    .search(',utt'));
            console.log("last: " + last);
            utt = xmlhttp.responseText.substring(xmlhttp.responseText
                    .search(',utt') + 5, xmlhttp.responseText
                    .search(',type'));
            console.log("utt: " + utt);
            type = "";
            attribute = "";
            out = "";
            index = "";
            values = "";
            if (xmlhttp.responseText.search(',att') > -1) {
                type = xmlhttp.responseText.substring(
                        xmlhttp.responseText.search(',type') + 6,
                        xmlhttp.responseText.search(',att'));
                //attribute
                pass = xmlhttp.responseText.substring(
                        xmlhttp.responseText.search(',att') + 5,
                        xmlhttp.responseText.search(',vals'));
                values = xmlhttp.responseText.substring(
                        xmlhttp.responseText.search(',vals') + 7,
                        xmlhttp.responseText.search(']}'));
            } else {
                type = 'instance';
                //index
                pass = xmlhttp.responseText
                        .substring(xmlhttp.responseText
                                .search('indexResult:') + 12,
                                xmlhttp.responseText.search(',inst:'));
                out = xmlhttp.responseText.substring(
                        xmlhttp.responseText.search('inst:') + 5,
                        xmlhttp.responseText.search('}'));
            }

            console.log("type: " + type);
            if (type === 'attribute') {
                vals = values.split(",");
                html = "You have  said the following to the virtual assistant: </br><i>'"
                        + utt
                        + "'</i></br></br>Given that you have  said this; what would be your preferred "
                        + attribute.toLowerCase().substring(0,
                                attribute.length - 1)
                        + "&#63</br></br><select id='choice' name='attribute'><option selected='selected' value='makeSelection'>please choose</option>";
                for (i = 0; i < vals.length; i++) {
                    html = html + "<option value='"+vals[i]+"'>"
                            + vals[i] + "</option>";
                }
                html = html
                        + "</select></br></br><input type='button' onclick='return sendChoice("
                        + first + "," + last + "," + utt + "," + type
                        + "," + pass + ")' value='Submit'></input>";
                document.getElementById("utteranceDiv").innerHTML = html;
            }
            if (type === 'instance') {
                html = "You have  said the following to the virtual assistant: </br><i>'"
                        + utt
                        + "'</i></br></br>Given that you have said this; </br> "
                        + out
                        + "</br><select id='choice' name='attribute'><option selected='selected' value='makeSelection'>please choose</option><option value='yes'>yes</option><option value='no'>no</option></select></br></br><input type='button' onclick='return  sendChoice("
                        + first
                        + ","
                        + last
                        + ","
                        + utt
                        + ","
                        + type
                        + "," + pass + ")' value='Submit'></input>";
                document.getElementById("utteranceDiv").innerHTML = html;
            }
        }
    }
    console
            .log("http://"
                    + server
                    + "/DialogueRefinementWebExperiment/WebExperimentServlet?first="
                    + first + "&last=" + last);
    xmlhttp
            .open(
                    "Get",
                    "http://"
                            + server
                            + "/DialogueRefinementWebExperiment/WebExperimentServlet?first="
                            + first + "&last=" + last, true);

    console.log('before');
    xmlhttp.send();
    console.log('after');
}

,另一个功能是:

function sendChoice(fname, lname, utterance, type, pass) {
    if (document.getElementById('choice').value === 'makeSelection') {
        alert('please make a choice');
        return false;
    } 
    else {
        choice = document.getElementById('choice').value;
        first = fname;
        last = lname;
        utt = utterance;
        typ = type;
        attOrInstIndex = pass;
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() //This part is actually executed last
        {
            console.log("within");
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) // Was the request processed successfully?
            {
                console.log(location);
                console.log(xmlhttp.responseText);
                type = xmlhttp.responseText.substring(
                        xmlhttp.responseText.search(',type') + 6,
                        xmlhttp.responseText.search(',att'));
                attribute = xmlhttp.responseText.substring(
                        xmlhttp.responseText.search(',att') + 5,
                        xmlhttp.responseText.search(',vals'));
                values = xmlhttp.responseText.substring(
                        xmlhttp.responseText.search(',vals') + 7,
                        xmlhttp.responseText.search(']}'));
                vals = values.split(",");
                console.log("type: " + type);
                if (xmlhttp.responseText.search(',att') > -1) {
                    type = xmlhttp.responseText.substring(
                            xmlhttp.responseText.search(',type') + 6,
                            xmlhttp.responseText.search(',att'));
                    //attribute
                    pass = xmlhttp.responseText.substring(
                            xmlhttp.responseText.search(',att') + 5,
                            xmlhttp.responseText.search(',vals'));
                    values = xmlhttp.responseText.substring(
                            xmlhttp.responseText.search(',vals') + 7,
                            xmlhttp.responseText.search(']}'));
                } else if (xmlhttp.responseText.search('instance') > -1) {
                    type = 'instance';
                    //index
                    pass = xmlhttp.responseText
                            .substring(xmlhttp.responseText
                                    .search('indexResult:') + 12,
                                    xmlhttp.responseText
                                            .search(',inst:'));
                    out = xmlhttp.responseText.substring(
                            xmlhttp.responseText.search('inst:') + 5,
                            xmlhttp.responseText.search('}'));
                } else {
                    type = 'done';
                }
                if (type === 'attribute') {
                    vals = values.split(",");
                    html = "You have  said the following to the virtual assistant: </br><i>'"
                            + utt
                            + "'</i></br></br>Given that you have  said this and the choices you have made so far; what would be your preferred "
                            + attribute.toLowerCase().substring(0,
                                    attribute.length - 1)
                            + "&#63;</br></br><select id='choice' name='attribute'><option selected='selected' value='makeSelection'>please choose</option>";
                    for (i = 0; i < vals.length; i++) {
                        html = html + "<option value='"+vals[i]+"'>"
                                + vals[i] + "</option>";
                    }
                    html = html
                            + "</select></br></br><input type='button' onclick='return sendChoice("
                            + first + "," + last + "," + utt + ","
                            + type + "," + pass
                            + ")' value='Submit'></input>";
                    document.getElementById("utteranceDiv").innerHTML = html;
                }
                if (type === 'instance') {
                    html = "You have  said the following to the virtual assistant: </br><i>'"
                            + utt
                            + "'</i></br></br>Given that you have said this and the choices you have made so far; </br> "
                            + out
                            + "</br><select id='choice' name='attribute'><option selected='selected' value='makeSelection'>please choose</option><option value='yes'>yes</option><option value='no'>no</option></select></br></br><input type='button' onclick='return sendChoice("
                            + first
                            + ","
                            + last
                            + ","
                            + utt
                            + ","
                            + type
                            + ","
                            + pass
                            + ")' value='Submit'></input>";
                    document.getElementById("utteranceDiv").innerHTML = html;
                } else {
                    //done make final Page.
                    html = "Thank you this scenario is over.";
                    document.getElementById("utteranceDiv").innerHTML = html;
                }
            }
        }
        xmlhttp
                .open(
                        "Post",
                        "http://"
                                + server
                                + "/DialogueRefinementWebExperiment/WebExperimentServlet",
                        true);
        //xmlhttp.setRequestHeader("accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");

        console.log('before');
        if (typ == "attribute") {
            xmlhttp.send("first=" + first + "&last=" + last
                    + "&mainType=" + type + "&att=" + attOrInstIndex
                    + "&utt=" + utt + "&choice=" + choice + "&done=no");
        } else if (typ == "instance") {
            xmlhttp.send("first=" + first + "&last=" + last
                    + "&mainType=" + type + "&indexResult="
                    + attOrInstIndex + "&utt=" + utt + "&choice="
                    + choice + "&done=" + choice);
        }
    }
}

HTML是:

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<script>
window.onload = function init() {
server = window.location.host;
params = location.search.split("?")[1];
first = params.split("&")[0].split("=")[1];
last = params.split("&")[1].split("=")[1];
}
</script>
</head>
<body>
<div id="utteranceDiv">
    When you are ready click Submit and the system will begin to ask you
    questions in an attempt to find you a restaurant that you are happy
    with.</br> <input type="button" onclick="return makeRequest()"
        value="Select"></input>
</div>
<script type="text/javascript">
 //see above for javascipt
</script>
</body>
</html>

第一次通话后div会更新:

<div id="utteranceDiv">
You have  said the following to the virtual  assistant: <br><i>'i want a   
mexican restaurant at 7 pm in cupertino'
</i><br><br>Given that you have said this; <br> How about null. It is in    
Mountain View; in Peninsula.  There is a reservation at earlyNight.<br>  
<select id="choice" name="attribute"><option selected="selected" 
value="makeSelection">please choose</option>
<option value="yes">yes</option>
<option value="no">no</option></select><br><br><input type="button" 
onclick="return sendChoice(eli,pincus,i want a mexican restaurant at 7 pm in 
cupertino,instance,1)" value="Submit"></div>

2 个答案:

答案 0 :(得分:0)

我检查你的帖子,但我找不到任何遗失的“)”。控制台不会告诉你在哪里看?

答案 1 :(得分:0)

我明白了;这是因为在更新的页面上写的javascript函数的参数是字符串,所以需要围绕它们的qoutes(其中一个有空格,这就是为什么我没有得到一个行号,它弄乱了解析器)< / p>