未捕获的SyntaxError:位于0 {J}的JSON中的意外标记D

时间:2017-01-13 07:10:50

标签: javascript php mysql json

这段代码在localhost中运行良好,但不知何故,当我上传到Linux服务器时,当我点击分区下拉菜单中的空白区域时,它在控制台显示数据错误(但没有值拉从它想要显示的数据库):

未捕获的SyntaxError:位于0的JSON中的意外标记D.     在JSON.parse()     在XMLHttpRequest.stateck(index.php:xxx) stateck @ index.php:xxx

第xxx行属于此代码: var myarray = JSON.parse(httpxml.responseText);

<!DOCTYPE html>
<?php
include "../connect.php";// connection to database 
?>
<html lang="en">
<head>

  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>  

<script>
    function checkYearDistrict()
    {
         var year=document.getElementById( "year" ).value;
         var district=document.getElementById( "s2" ).value;

         if(year, district)
         {
              $.ajax({
              type: 'post',
              url: 'checkdata.php',
              data: {
                    yearID:year,
                    districtID:district,
                    },
              success: function (response) {
                    console.log(response );

                    if (response == 'false') {
                        $( '#year_status' ).html("Record Not Exist!");
                        $( '#district_status' ).html("<span>Record Not Exist!</span>");                     
                    } else {
                        json = JSON.parse(response);
                        $( '#year_status' ).html("OK");
                        $( '#district_status' ).html("<span style='color:red'>Record Already Exist.</span>");                       
                    }                     
              }
              });
         }
         else
         {

              $( '#district_status' ).html("");
              return false;
         }
    }



    function checkall()
    {        
         var yearhtml=document.getElementById("year_status").innerHTML;
         var districthtml=document.getElementById("district_status").innerHTML;

         if((yearhtml && districthtml)=="OK")
         {
            return true;
         }
         else
         {
            return false;
         }
    }

</script>

  </head>
<body>

<div class="container">

  <form name="testform" class="form-horizontal" method="POST" onsubmit="return checkall();>

    <div class="form-group">
    <div class="input-group">
    <span class="input-group-addon search_text_width">Year:</span>  
    <select id="year" name="year" class="form-control select2">
      <option value="">Select Year</option>
        <script>
            var myDate = new Date();
            var year = myDate.getFullYear();
            for(var i = 1999; i < year+1; i++){
                document.write('<option value="'+i+'">'+i+'</option>');
            }                   

        </script>

        <script type="text/javascript">
            function AjaxFunction()
            {
                var httpxml;
                try
                  {
                  // Firefox, Opera 8.0+, Safari
                  httpxml=new XMLHttpRequest();
                  }
                catch (e)
                {
                  // Internet Explorer
                    try
                    {
                         httpxml=new ActiveXObject("Msxml2.XMLHTTP");
                    }
                    catch (e)
                    {
                        try
                        {
                            httpxml=new ActiveXObject("Microsoft.XMLHTTP");
                        }
                        catch (e)
                        {
                            alert("Your browser does not support AJAX!");
                            return false;
                        }
                    }
                }

                function stateck() 
                {
                    if(httpxml.readyState==4)
                    {
                        //alert(httpxml.responseText);
                        var myarray = JSON.parse(httpxml.responseText);
                        // Remove the options from 2nd dropdown list 
                        for(j=document.testform.dist.options.length-1;j>=0;j--)
                        {
                            document.testform.dist.remove(j);
                        }


                        for (i=0;i<myarray.data.length;i++)
                        {
                            var optn = document.createElement("OPTION");
                            optn.text = myarray.data[i].dis_name;
                            optn.value = myarray.data[i].dis_id;  // You can change this to subcategory 
                            document.testform.dist.options.add(optn);

                        } 
                    }
                } // end of function stateck

                var url="dd.php";
                var div_code=document.getElementById('s1').value;
                url=url+"?div_code="+div_code;
                url=url+"&sid="+Math.random();
                httpxml.onreadystatechange=stateck;
                //alert(url);
                httpxml.open("GET",url,true);
                httpxml.send(null);
              }
        </script>  
    </select>   
    </div>  

    <br>
    <?php   require "config.php";   ?>
    <div class="input-group">
    <span class="input-group-addon search_text_width">Division</span>
        <select name="div" id="s1" class="demoInputBox form-control" onchange="AjaxFunction();">
            <option value="">Select Division</option>
            <?php
            $sql="select * from division ";  

            foreach ($dbo->query($sql) as $row) 
            {
                echo "<option value=$row[div_code]>$row[div_name]</option>";
            }
            ?>
        </select>
    </div>
    <br>
    <div class="input-group">
        <span class="input-group-addon search_text_width">District</span>
        <select name="dist" id="s2" class="demoInputBox form-control" onchange="checkYearDistrict();">
            <option value="">Select District</option>
        </select>       
    </div>
    <center><span id="district_status"></span></center>

    <br>
    <div class="input-group">
        <div class="col-sm-offset-3 col-sm-10">
            <input type="submit" class="btn" name="submit" value="Save Record">
        </div>
    </div>
  </form>
</div>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

您可以要求text/ html数据

,而不是JSON格式的回复

只需在ajax调用中添加一个属性DataType: JSON即可 并使用JSON

中的json_encode()PHP格式发送回复
$.ajax({
  type:"POST",
  url:"test.php",
  data:{data:'data'},
  dataType: 'json',
  success: function(data){
    if(data['success']=='true'){
        alert('success');
    }else{
      if(data['success']=='false'){
        alert("not success");
      }
    }
  }
});

<强> test.php的

   if($_POST['data']==1){
      $response['success'] = "true";
   } else {
      $response['success'] = "false";
   }

echo json_encode($response);

答案 1 :(得分:0)

由于函数返回“ return VoucherNo;”,我遇到了相同的错误。将我更改为“返回新的JavaScriptSerializer()。Serialize(VoucherNo);”之后,它不会显示错误。

            public string SaveDetails(string Mode)
        {
            int res = 0;
            int res1 = 0;
            string VoucherNo = "";
            try
            {

            }
            catch (Exception ex)
            { }

            //return VoucherNo;
            return new JavaScriptSerializer().Serialize(VoucherNo);
        }