简体   繁体   English

Javascript Uncaught SyntaxError:意外的令牌

[英]Javascript Uncaught SyntaxError: Unexpected token

I am having issues calling my javascript function which loops through an array calling an external PHP page for each value.我在调用我的 javascript 函数时遇到问题,该函数循环遍历一个数组,为每个值调用一个外部 PHP 页面。 I get the following error in my developer console in Chrome:我在 Chrome 的开发者控制台中收到以下错误:

Uncaught SyntaxError: Unexpected token .未捕获的 SyntaxError: Unexpected token 。 CSV.php?reg=1:3 CSV.php?reg=1:3

When inspecting my values passing to the script everything is there as it should be:当检查我传递给脚本的值时,一切都应该是这样的:

<script type="text/javascript">
function.csvgen(){
var area = "["22","23","24"]";
var start =""2017-01-30"";     
var end = ""2017-02-06"";
var len = area.length;
for (i = 0; i < len; i++) { 
$.getScript("CSVGEN.php?area="+area[i]+"&start="+start+"&end="+end);
}
}
</script>

I'm not exactly a good programmer and have used very little Javascript (which required assistance from this wonderful forum as well...).我不是一个好的程序员,使用的 Javascript 很少(这也需要这个精彩论坛的帮助......)。 Here is my code for the page.这是我的页面代码。 The point of the code is to let the user select a number of areas based on their region as well as a start date and an end date and then generate a CSV from my MS SQL database for each, the code for which is in the called CSVGEN.PHP file.代码的重点是让用户根据他们的地区以及开始日期和结束日期选择多个区域,然后从我的 MS SQL 数据库中为每个区域生成一个 CSV,其代码在被调用的CSVGEN.PHP 文件。 I've tested the CSVGen file with a manually generated link and it works, it does not if I put the static link inside the for loop.我已经使用手动生成的链接测试了 CSVGen 文件并且它可以工作,如果我将静态链接放在 for 循环中,则不会。

<script type="text/javascript">
function.csvgen(){
    var area = "<?php echo json_encode(array_values($_POST['arealist'])); ?>";
    var start ="<?php echo json_encode($_POST['start']); ?>";     
    var end = "<?php echo json_encode($_POST['end']); ?>";
    var len = area.length;
    for (i = 0; i < len; i++) { 
        $.getScript("CSVGEN.php?area="+area[i]+"&start="+start+"&end="+end);
    }
}
</script>

<?php
$page_title="CSV Generator";
include("\Include\header.inc");
include("\Include\connect-db.php");
include("\Include\CSVGen.php");

$error="";

$start_date=date("Y-m-d");
$end_date=date("Y-m-d", strtotime("+7 days"));


if(isset($_GET['reg'])) 
{
    $reg=$_GET['reg'];
}

else{
    $reg='1';
}


if($start_date>$end_date){
    $error = 'ERROR: End Date cannot be before Start Date!';
}

if ($error != '')
{

    echo '<div class="container">
        <div class="row">
        <div class="alert alert-danger col-md-12">'.$error.'
        </div>
        </div>
        </div>';

}
$sqlareas="SELECT Area_Name, Region_ID, Area_ID FROM Listings_Areas WHERE region = '$reg'";
$arearesult= sqlsrv_query($conn, $sqlareas, array(), array("Scrollable"=>"buffered"));
$areacount = sqlsrv_num_rows($arearesult);


function renderForm($arearesult, $areacount, $start_date, $end_date){
?>

<html>

<head>

</head>

<body>


<div class="container">

    <div class="row">
        <form id="CSV" name="form1" method="post">
            <div class="col-md-2 col-md-offset-1">

                <p><select name="arealist[]" size="<?php echo $areacount ;?>" multiple="multiple" tabindex="1">

                <?php

                while($areas=sqlsrv_fetch_array($arearesult)){
                    echo'<option value="' . $areas['Area_ID'] . '">' . $areas['Area_Name'] . '</option>';
                }
                ?>
                </select>
            </div>

            <div class="col-md-3">   
                <strong>Start Date: </strong> <input type="date" name="start" value="<?php echo $start_date; ?>" />
            </div>
            <div class="col-md-3">  
                <strong> End Date: </strong> <input type="date" name="end" value="<?php echo $end_date; ?>" />
            </div>
            <div class="col-md-2">  
                <input type="submit" onclick="csvgen()" name="submit" value="Get CSVs">
            </div>
        </form>
    </div>
</div>

<?php
}
if($_SERVER['REQUEST_METHOD'] === 'POST'){
print_r(array_values($_POST['arealist']));
echo $_POST['start'];
echo $_POST['end'];
}
else{    
renderForm($arearesult, $areacount, $start_date, $end_date);
}
?>

I've tried removing all tabbing/spacing and clearing any potential illegal characters that might have snuck in, but it's showing a period, which I can only guess is referring to either my area.length which as far as I can tell from the manual is right and I still get the error if I remove it or $.getscript, but I've used that elsewhere in similar functions with no issue so I don't know why that would be wrong, or how to replace it.我已经尝试删除所有制表符/间距并清除任何可能潜入的潜在非法字符,但它显示了一个句点,我只能猜测它指的是我的 area.length,据我从手册中可以看出是对的,如果我删除它或 $.getscript,我仍然会收到错误,但我已经在类似函数的其他地方使用过它,没有问题,所以我不知道为什么会出错,或者如何替换它。

At the very begining of the script you have:在脚本的最开始,你有:

<script type="text/javascript">
function.csvgen(){
    //...

which should be:应该是:

<script type="text/javascript">
function csvgen(){
    //...

with a space instead .用空格代替. between function and csvgen .functioncsvgen之间。

NOTE: this area = "["22","23","24"]";注意:这个area = "["22","23","24"]"; is also wrong.也是错的。 Use diferent quotes (like area = '["22","23","24"]'; ) or escape the inner quotes (like area = "[\\"22\\",\\"23\\",\\"24\\"]"; )使用不同的引号(如area = '["22","23","24"]'; )或转义内部引号(如area = "[\\"22\\",\\"23\\",\\"24\\"]"; )

Find a good javascript tutorial and learn more about how to declare function in javascript.找到一个好的 javascript教程并了解有关如何在 javascript 中声明函数的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM