SyntaxError:ajax调用中的意外标记l

时间:2014-01-30 11:24:14

标签: javascript php android jquery ajax

我正在尝试从服务器数据库中获取数据并将其传递给ajax,以在本地android数据库中创建数据库表及其数据。但是当ajax调用时,它会产生以下错误。 logcat的:

01-30 10:58:45.888: D/CordovaLog(31914): Server is not responding... Please try again: SyntaxError: Unexpected token l
01-30 10:58:45.888: I/Web Console(31914): Server is not responding... Please try again: SyntaxError: Unexpected token l at file:///android_asset/www/home.html:513

这里是ajax代码:

$.ajax({
                url : urlServer + 'getTableData.php',
                //      type: 'POST',
                contentType : 'application/json',
                beforeSend : function() {
                    $.mobile.loading('show')
                },
                complete : function() {
                    console.log("ajax complete");
                    createTable();
                },
                dataType : 'json',
                data : {userId: user_id},
                success : function(data) {
                    if (data != null)
                    {
                        dynamic_tabledetails = data.Table_details;
                        dynamic_selectQuery = data.SelectTableQuery;
                        table_data = data;
                        getTabledetails(dynamic_tabledetails);                  
                    } 
                    else 
                    {
                        alert("Error Message");
                    }
                },
                error : function(xhr, ajaxOptions, thrownError) {
                    console.log("Server is not responding... Please try again: "+thrownError);
                }
            });

这是php代码:

<?php
require_once ('connect.php');
$userID= $_REQUEST['userId'];
$data = array ();
$listtables = array();
$Tabledetails = array();
$select_table = '';
$tab_name = array();
$getlistTables = 'SHOW TABLES FROM sacpl_crm_dev ';
$resultsListTables = mysql_query($getlistTables);
echo 'length of the tables name: '.$resultsListTables.' ';
while ($row = mysql_fetch_array($resultsListTables))
{

if(strpos($row[0],'_trail') == false)
{

$temporarydata = array();
$TableName = new ArrayObject();
$getTabledetails = 'show columns from '.$row[0].'';
$resultdetails = mysql_query($getTabledetails);
$TableName['tablename'] = $row[0];
$tab_name[] =$row[0]; 
$column = array();
 $delete_field = '';
 $comp_codeField = '';
while($rows = mysql_fetch_array($resultdetails))
{
    $column_list =new ArrayObject();
    $column_list['FieldName'] = $rows['Field'];
        $column_list['Default'] = $rows['Default'];
        if(strpos($rows['Type'],'(') == false)
        {
            $column_list['dataType'] = $rows['Type'];
            $column_list['dataType_limit'] ='';
        }
        else
        {
            $type = explode('(',$rows['Type']);
            $column_list['dataType'] = $type[0];
            $column_list['dataType_limit'] = '('.$type[1];
        }
        if($rows['Field'] == 'deleted')
        {
            $delete_field = 'deleted = 0';
        }
        if($rows['Field'] == 'userId')
        {
            $userIdField = $rows['Field'].'="'.$userId.'"';
        }

    $column_list['Extra'] = $rows['Extra'];
    $column_list['Null_value'] = $rows['Null'];
    $column_list['Key_value'] = $rows['Key'];
     $column[] = $column_list;
}

 $TableName['column_details'] = $column;
 $Tabledetails[]=$TableName;
 if($userIdField == '' && $delete_field !='')
 {
    $select_table = 'select * from '.$row[0].' where '.$delete_field.'';
 }
 else if($userIdField != '' && $delete_field =='')
 {
    $select_table = 'select * from '.$row[0].' where '.$userIdField.'';
 }
 else if($userIdField != '' && $delete_field !='')
 {
    $select_table = 'select * from '.$row[0].' where '.$userIdField.' and '.$delete_field.'';
 }
 else{
    $select_table = 'select * from '.$row[0].'';
 }
 $select_query[] = $select_table;

$resultTableData = mysql_query($select_table);
while ($row1 = mysql_fetch_array($resultTableData))
{
    $temporarydata[] = $row1;
}   
$data[$row[0]] = $temporarydata;    

}
}

$data['Table_details'] = $Tabledetails;
$data['SelectTableQuery'] = $select_query;
mysql_close($con);
require_once('JSON.php');
$json = new Services_JSON();
echo ($json->encode($data));
?>

3 个答案:

答案 0 :(得分:0)

注释掉这一行:

echo 'length of the tables name: '.$resultsListTables.' ';

此外,在为AJAX调用输出JSON时,使用以下方法设置Content-type标头非常重要:

header('Content-type: application/json; charset=utf-8',true);

答案 1 :(得分:0)

这个PHP代码似乎没有语法错误。问题可能在于包含的php:“connect.php”和“JSON.php”。你能不能发布它们,以便我们找到错误。

答案 2 :(得分:0)

将其链接到PHP文件的开头:

  

标题(&#34;内容类型:text / javascript; charset = utf-8&#34;);