简体   繁体   English

VM299:1 Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous> )

[英]VM299:1 Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>)

I am trying to get data using ajax request following is ajax request.我正在尝试使用 ajax 请求获取数据,下面是 ajax 请求。

document.getElementById("myForm").style.display = "block";
                    $('body').css('overflow','hidden');
                    $.ajax({  
                        type: 'POST',  
                        url: 'techtest.php', 
                        dataType: 'json',
                        data: { id: val },
                        success: function(response, status, xhr){ 
                            console.log(response.array[1]);
                            document.getElementById('tech_about_me').innerHTML = response.array[0];
                            document.getElementById('tech_assest_group').innerHTML = response.array[1];
                            document.getElementById('tech_skill_group').innerHTML = response.array[2]+""+response.array[3];
                            document.getElementById('tech_address').innerHTML = response.array[4];


                        },
                        error: function(data,XMLHttpRequest,xhr, textStatus, errorThrown) { 
                            console.log("Status: " + textStatus); console.log("Error: " + errorThrown); 
                            var err =  JSON.parse(xhr.responseText);
                            console.log(err.Message);

                            var json = $.parseJSON(data);
                            alert(json.error);
                        }  
                    });

But I am receiving error " VM299:1 Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse ()"但我收到错误“VM299:1 Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse ()”

following is my code to return json以下是我返回json的代码

<?php
 header("Content-Type: application/json");
 include "../common/config.php";
 include"../includes.php";
 include"session.php";
 // print_r('<script>console.log('.$_POST['id'].')</script>');
 $technician_details=$db->get_a_line("select * from user where user_id='".$_POST['id']."'");
 $about_me=$technician_details['about_me'];
 $address=$technician_details['address'];
 $assest_group=$technician_details['assest_group'];
 $skill_group=$technician_details['skill_group'];
 $skill_list=$technician_details['skill_list'];
 $data = array();
 $data[]=$about_me;
 $data[]=$assest_group;
 $data[]=$skill_group; 
 $data[]=$skill_list;
 $data[]=$address;
 $json_data = array(

        "recordsTotal"    => '5',  // total number of records
        "array"            => $data   // total data array
        );

 echo json_encode($json_data);  // send data as json format
 ?>

It was working before.它以前工作过。 but while testing yesterday i noticed it is giving error on pc's other than my local.但是在昨天测试时,我注意到它在我本地以外的 PC 上出错。 from tomorrow morning it started giving same error on local to.从明天早上开始,它开始在本地出现同样的错误。 Please help out.请帮忙。

After your last comment I quickly put together a small test to emulate what your script is doing.在您发表最后一条评论后,我迅速组织了一个小测试来模拟您的脚本正在做什么。 This works OK.这工作正常。

I suggest testing as-is first then, if you are happy that it is ok, uncomment the include lines and test again.我建议先按as-is测试,如果您觉得没问题,请取消注释include行并再次测试。 Then, if that works try using the actual db query etc然后,如果可行,请尝试使用实际的数据库查询等

<?php
    if( $_SERVER['REQUEST_METHOD']=='POST' ){
        if( !empty( $_POST['id'] ) ){
            /*

            # are these being included correctly?
            include '../common/config.php';
            include '../includes.php';
            include 'session.php';


            */


            # to emulate db query and results
            $id=filter_input( INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT );
            $sql=sprintf( 'select * from user where user_id="%s"',$id );

            $payload=array(
                'about'         =>  'From db - all about user - based upon id '.$id,
                'assetgroup'    =>  'From db - which asset group',
                'skillgroup'    =>  'From db - which skill group',
                'skilllist'     =>  'from db - what skills',
                'address'       =>  'from db - address of tech'
            );
            $data = array(
                'recordsTotal'  =>  count( $payload ),
                'data'          =>  $payload,
                'sql'           =>  $sql
            );
            header( 'Content-Type: application/json' );
            exit( json_encode( $data ) );
        }
        exit('error');
    }
?>
<!DOCTYPE html>
<html lang='en'>
    <head>
        <meta charset='utf-8' />
        <title>basic jquery - error parsing response</title>
        <script src='//code.jquery.com/jquery-latest.js'></script>
        <script>
            document.addEventListener('DOMContentLoaded',()=>{

                const node=function(id){
                    return document.getElementById( id )
                };

                let val=303;

                $.ajax({  
                    type:'POST',  
                    url:location.href,  //techtest.php
                    dataType:'json',
                    data:{ id: val },
                    success:function( r ){
                        node('tech_about_me').innerHTML = r.data.about;
                        node('tech_assest_group').innerHTML = r.data.assetgroup;
                        node('tech_skill_group').innerHTML = r.data.skillgroup+" "+r.data.skilllist;
                        node('tech_address').innerHTML = r.data.address;

                        console.info( r.sql, r.recordsTotal )
                    },
                    error: function(e) { alert(e); }  
                });
            });
        </script>
    </head>
    <body>
        <div id='tech_about_me'></div>
        <div id='tech_assest_group'></div>
        <div id='tech_skill_group'></div>
        <div id='tech_address'></div>
    </body>
</html>

暂无
暂无

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

相关问题 SyntaxError:JSON中的意外令牌u在JSON.parse( <anonymous> ) - SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>) Uncaught SyntaxError: JSON.parse (<anonymous> ) 在 Response.Body.json</anonymous> - Uncaught SyntaxError: Unexpected token U in JSON at position 0 at JSON.parse (<anonymous>) at Response.Body.json 未捕获到的SyntaxError:JSON中的意外令牌u在JSON.parse的位置0 - Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse 未捕获的SyntaxError:JSON.parse中位置0的JSON中的意外标记a( <anonymous> ) - Uncaught SyntaxError: Unexpected token a in JSON at position 0 at JSON.parse (<anonymous>) 未捕获到的SyntaxError:JSON中的意外令牌&lt;在JSON.parse位置0处( <anonymous> ) - Uncaught SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) Uncaught SyntaxError: Unexpected token & in JSON at position 1 at JSON.parse (<anonymous> )</anonymous> - Uncaught SyntaxError: Unexpected token & in JSON at position 1 at JSON.parse (<anonymous>) 未捕获的语法错误:JSON 中的意外令牌 u 在 JSON.parse 的 position 0 处(<anonymous> )</anonymous> - Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>) 未捕获的语法错误:JSON 中的意外令牌 u 在 JSON.parse 的 position 0 处(<anonymous> ) at./src/context/AuthContext.js</anonymous> - Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>) at ./src/context/AuthContext.js Uncaught SyntaxError: Unexpected token &lt; in JSON at position 0 : at JSON.parse (<anonymous> ) 在对象。<anonymous> - Uncaught SyntaxError: Unexpected token < in JSON at position 0 : at JSON.parse (<anonymous>) at Object.<anonymous> 语法错误:在 JSON 中的意外令牌 u 在 position 0 在 JSON.parse - SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM