为什么我收到错误意外的令牌<在JSON的位置0 parsererror?

时间:2016-08-22 07:58:04

标签: php json

所有我都有一个表单将数据提交到PHP文件,该文件将发布数据插入CSV文件,我升级此表单以使用ajax,这样用户就不必刷新页面提交后,

但是我遇到问题我得到错误

  

意外的令牌<在JSON的第0位parsererror

并且我不完全确定这意味着我已经在我的开发服务器上测试了这个表单并且它起了作用并且我复制了它并且做了一些微小的调整表单ID其字段以验证等,但是现在我得到错误,我不知道为什么,任何想法?

  <form id="form2" name="form2">
    <div id="response"></div>
    <table>
        <tr><td><input type="text" name="fname" id="fname" placeholder="First Name"/></td><td><input type="text" name="lname" id="lname" class="required" placeholder="Surname"/></td></tr>
        <tr><td><input type="text" name="email" id="email" placeholder="Email"/></td><td><input type="text" name="postcode" id="postcode" placeholder="Postcode"/></td></tr>
        <tr><td colspan="2">
            <p>Safe Combination</p>
            <select name="number1">
                <option value="0">0</option>
                <option value="1">1</option>
                ...
            </select>
            <select name="number2">
                <option value="0">0</option>
                <option value="1">1</option>
                ...
            </select>
            <select name="number3">
                <option value="0">0</option>
                <option value="1">1</option>
                <option value="2">2</option>
                ...
            </select>
            <select name="number4">
                <option value="0">0</option>
                <option value="1">1</option>
                ...
            </select>
            <select name="number5">
                <option value="0">0</option>
                <option value="1">1</option>
                ...
            </select>
            <select name="number6">
                <option value="0">0</option>
                <option value="1">1</option>
                ...
            </select></td></tr>
        <tr><td colspan="2"><input class="btn btn-danger" type="submit" value="SUBMIT" id="submit" name="submit"/></td></tr>
    </table>
</form>

JS

<script src="https://code.jquery.com/jquery-3.1.0.min.js" integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s=" crossorigin="anonymous"></script>
        <script type="text/javascript">
            $(document).ready( function(){
                $('form#form2').submit(function (e) {

                    e.preventDefault();

                    // Get the Login Name value and trim it
                    var fname = $.trim($('#fname').val());
                    var lname = $.trim($('#lname').val());
                    var email = $.trim($('#email').val());
                    var postcode = $.trim($('#postcode').val());
                    // Check if empty of not

                    var valid = '';

                    if (fname  === '') {
                        //alert('First Name is empty.');
                        valid+ 'First Name is empty.'; 
                        //return false;
                    }
                    if (lname  === '') {
                        //alert('Last Name is empty.');
                        valid+ 'Last Name is empty.'; 
                        //return false;
                    }
                    if (email  === '') {
                        //alert('Email is empty.');
                        valid+ 'Email is empty.'; 
                        //return false;
                    }
                    if (postcode  === '') {
                        //alert('Postcode is empty.');
                        valid+ 'Postcode is empty.'; 
                        //return false;
                    }
                    if(valid != '') {
                        $('#form2 #response').addClass('alert alert-danger').html("Errors - " + valid)
                    } else {
                        $('#form2 #response').removeClass('alert alert-danger').addClass('alert alert-warning').html("<i class='fa fa-spinner fa-spin'></i> Processing the form");
                        var formData = $('#form2').serialize();
                        submitForm(formData);
                    }
                });
            });

            function submitForm(formData) {
                $.ajax({
                    type: 'POST',
                    url: 'comp/checker.php',
                    data: formData,
                    dataType: 'json',
                    cache: false,
                    timeout:  7000,
                    success: function(data) {

                        $('#form2 #response').removeClass('alert alert-warning').addClass((data.error === true) ? 'alert alert-danger' : 'alert alert-success').html(data.msg);
                    }, 
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        $('#form2 #response').html('Something else went wrong :/ ' + errorThrown + textStatus);
                    },
                    complete: function(XMLHttpRequest, status) {
                        $('#form2')[0].reset();         
                    }
                });
            }


        </script>

PHP文件

    <?php
    //header('Content-type: application/json');
    sleep(5);

    //if (!empty($_POST)) {


        $date = date("d/m/Y");
        $fname = trim($_POST['fname']);
        $lname = trim($_POST['lname']);
        $email = trim($_POST['email']);
        $postcode = trim($_POST['postcode']);

        $number1 = $_POST['number1'];
        $number2 = $_POST['number2'];
        $number3 = $_POST['number3'];
        $number4 = $_POST['number4'];
        $number5 = $_POST['number5'];
        $number6 = $_POST['number6'];

        $totalnumber = $number1 . '' . $number2 . '' . $number3 . '' . $number4 . '' . $number5 . '' . $number6;

        $list = array("$date, $fname, $lname, $email, $postcode, $totalnumber");

        $file = fopen("contacts_2.csv","a");

        foreach ($list as $line) {fputcsv($file,explode(',',$line));}

        fclose($file);

        //HEADER TO INDEX.PHP


        $return['error'] = false;
        $return['msg'] = "<p>Thanks for your feedback</p>";
        echo json_encode($return);
        exit();

    //} else {
    //  $return['error'] = true;
    //  $return['msg'] = "<p>Something went wrong, please retry in a few minutes</p>";
    //  echo json_encode($return);
    //  exit();
    //}

    ?>

3 个答案:

答案 0 :(得分:1)

您从服务器收到的响应不是有效的json。 您可能正在以xml格式获取响应。 这可能是由于php页面生成错误。检查响应是否由php页面生成。还要检查php页面是否有足够的权限在生产服务器上执行。

检查浏览器控制台以查看php文件生成的响应。

答案 1 :(得分:0)

我一直收到这个错误:

  

&#34;意外的令牌&lt;在JSON的位置0&#34;

解决方案:

  • 我从javascript(通过ajax with dataType: "json",)向php发送了一个号码。
  • 我没有意识到这个数字会在到达php之前转换为字符串。
  • 因此,为了在php端使用该发送的值进行查询等,我需要使用intval将其转换回数字:

    $gender = intval($_POST["gender"]);

答案 2 :(得分:0)

如果Wordpress尝试使用此功能:wp_send_json()对我有用。