【问题标题】:JSON.parse single variable - Uncaught SyntaxError: Unexpected token < in JSONJSON.parse 单个变量 - 未捕获的 SyntaxError:JSON 中的意外标记 <
【发布时间】:2020-03-31 13:45:14
【问题描述】:

我从 Ajax 开始,不幸的是我遇到了 JSON.parse() 的问题。我想通过点击传递一个变量。

到目前为止,我还没有找到具体问题的答案。也许这里有人可以帮助我。

错误信息:

Uncaught SyntaxError: Unexpected token < in JSON at position 68
at JSON.parse (<anonymous>)
at Object.<anonymous> (ajax.js?ver=5.3.2:13)
at c (jquery.js:2)
at Object.fireWith [as resolveWith] (jquery.js:2)
at l (jquery.js:2)
at XMLHttpRequest.<anonymous> (jquery.js:2)

我的 JS 函数:/admin/script/ajax.js

    $("#make").change(function(){

        var make = $('#make').val();
        console.log('Select Value hat sich geändert zu ' + make);

        $.ajax({
                url: wpAjax.ajaxUrl,
                data: 'make=' + make,
                type: 'post'
            }).done(function(model){
            model = JSON.parse(model);   // Here comes the error.
        })
    })
})

我的 PHP 函数:admin/script/filter.php

<?php
  if(isset($_POST['make'])) {
     $model = 'Model S';
     echo json_encode($model);
  }
?>

主题中的Wordpress functions.php

function my_enqueue() {

    wp_enqueue_script( 'ajax-script', get_template_directory_uri() . '/admin/script/ajax.js', array('jquery') );

    wp_localize_script( 'ajax-script', 'wpAjax',
            array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}
add_action( 'wp_enqueue_scripts', 'my_enqueue' );

get_template_part( 'admin/script/filter');

【问题讨论】:

  • 使用 console.log(model); 检查来自脚本的响应,或检查开发者控制台的网络选项卡。您没有从服务器获得正确的 JSON。可能是错误或其他原因。
  • 您的 JSON 实际上是干净的 JSON 吗?你的 JSON 是空的吗?
  • console.log(model) 先看初始数据;以及您需要如何处理它,您可能不需要进行解析
  • @emilioestevez — 它当然不是空的。它在第 68 位有一个&lt;
  • 请显示完整的php代码。它似乎发送了冗余数据作为响应

标签: php ajax wordpress


【解决方案1】:

一旦你使用了wordpress,首先你需要将php函数绑定到action

说动作名称是get_model

add_action( 'wp_ajax_get_model', 'my_func' ); // Here action name tied to function
add_action( 'wp_ajax_nopriv_get_model', 'my_func' ); // Here too for unregistered users

function my_func(){
    if(isset($_POST['make'])) {
        $model = 'Model S';
        echo json_encode($model);
    } else {
        die('No models');
    }
}

那么你需要在javascript中将动作名称定义为ajax参数

$("#make").change(function(){
    var make = $('#make').val();
    console.log('Select Value hat sich geändert zu ' + make);
    $.ajax({
            url: wpAjax.ajaxUrl,
            data: {
                  'make': make,
                   action: 'get_model'},  // add action name here
            type: 'post'
        }).done(function(model){
        model = JSON.parse(model);   // Here comes the error.
    })
})

现在我怀疑它必须运行良好。请测试

您可以在浏览器开发控制台中查看发布参数

一旦失败,您可以在开发控制台的“预览”和“响应”选项卡中找到真正的响应内容(之前打开“XHR”过滤器)

【讨论】:

  • 我刚刚复制了您的代码并将 jquery 放入 $(document).ready(function(){....}) 并将您的第一个代码放入 functions.php 并删除了我的“get_template_part( '管理员/脚本/过滤器');"从functions.php。但仍然得到一个错误: Uncaught SyntaxError: Unexpected token
  • 我可以添加它的工作直到“model = JSON.parse(model);”。
  • 这是因为错误的响应来了。检查你的functions.php - 它可能有多余的输出,就像include file.php一样,它与html相呼应,或者functions.php中有一种语法错误
  • 我添加了另一张图片来回答。看看
  • 不清楚为什么在functions.php中使用get_template_part。你不介意在标题或任何模板内部这样做吗?本质上,functions.php 不应该生成任何直接输出。您应该将任何输出包装到函数中,然后按需调用它
猜你喜欢
  • 2023-03-23
  • 2016-10-22
  • 2013-01-04
  • 1970-01-01
  • 2023-03-05
  • 2020-01-10
  • 1970-01-01
相关资源
最近更新 更多