【问题标题】:Uncaught SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse未捕获的语法错误:JSON.parse 中位置 0 处的 JSON 中的意外标记 <
【发布时间】:2018-03-29 21:25:18
【问题描述】:

当我尝试使用 JSON.parse 解析响应文本时,我收到此错误:

Uncaught SyntaxError: Unexpected token

任何时候我运行下面的代码。

javascript/ajax 代码

    function calculateMeasurements() {
        clearResult();
        clearErrors();

        var form = document.getElementById("measurement-form");
        var action = form.getAttribute("action");

        // gather form data
        var form_data = new FormData(form);
        for ([key, value] of form_data.entries()) {
            console.log(key + ': ' + value);
        }

        var xhr = new XMLHttpRequest();
        xhr.open('POST', action, true);
        // do not set content-type with FormData
        //xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
        xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                var result = xhr.responseText;
                var json = JSON.parse(result);
                if (json.hasOwnProperty('errors') && json.errors.length > 0) {
                    displayErrors(form, json.errors);
                } else {
                    postResult(json.volume);
                }
            }
        };
        xhr.send(form_data);
    }

    var button = document.getElementById("ajax-submit");
    button.addEventListener("click", calculateMeasurements);

})();

process.php

<?php 
function is_ajax_request(){
    return $_SERVER["HTTP_X_REQUESTED_WITH"] && $_SERVER["HTTP_X_REQUESTED_WITH"] == "XMLHttpRequest";
}

  $length = isset($_POST['length']) ? (int) $_POST['length'] : '';
  $width = isset($_POST['width']) ? (int) $_POST['width'] : '';
  $height = isset($_POST['height']) ? (int) $_POST['height'] : '';

  $errors = [];

  if(empty($length)){$errors[] = "length";}

  if(empty($width)){$errors[] = "width";}

  if(empty($height)){$errors[] = "height";}

 if(!empty($errors)){
     $result_array = array('errors' => $errors);
     echo json.encode($result_array);
     exit;
 }

 $volume = $length * $width * $height;  
  if(is_ajax_request()) {
    echo json.encode(array('volume' => $volume));
  } else {
    exit;
  }

?>

每当我对从 ajax 响应获得的结果变量使用 JSON.parse 时,我就注意到了这个错误。

【问题讨论】:

    标签: ajax


    【解决方案1】:

    我不认为他们的 JavaScript 代码有什么问题。请尝试正确使用php函数。应该是这样的

    echo json_encode(array('volume' => $volume)); 
    
    echo json_encode($result_array);
    

    而不是:

    echo json.encode(array('volume' => $volume)); // json.encode as you have used in your code is wrong.
    
    echo json.encode($result_array) // json.encode as you have used in your code is wrong.
    

    一旦进行此更改,它应该可以正常工作

    【讨论】:

    • 感谢 Onome Mine Adams。在您进行更正后。我的代码运行良好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-13
    • 2017-06-27
    • 1970-01-01
    • 2022-06-17
    • 1970-01-01
    • 2023-03-09
    • 2017-10-21
    相关资源
    最近更新 更多