【问题标题】:"SyntaxError: JSON.parse: unexpected character" Error when passing multiple variables from AJAX to PHP“SyntaxError:JSON.parse:意外字符”将多个变量从 AJAX 传递到 PHP 时出错
【发布时间】:2014-11-11 05:48:06
【问题描述】:

我正在使用 AJAX 将变量从表单传递到 PHP 页面以处理数据库中的数据。

一旦用户点击一个按钮,它就会触发以下 JavaScript:

$(document).ready(function() {

    $("#myForm").submit(function(event) {

        /* validate the fields */
        var firstDate= "11/10/2014"
        var secondDate = "10/10/2014"
        var myString = "some Text";
        var myArray = ["name1", "name2", "name3", "123-123-33gf"];

        processIT(firstDate, secondDate, muString, myArray);

    });/* end of submit */

});

function processIT(firstDate, secondDate, muString, myArray) {
    var response = ""; 
    $(function () {
        $.ajax({
            url: 'api.php',           // the script to call to get data
            type: "POST", 
            data: {
                firstDate: firstDate, 
                secondDate : secondDate , 
                myString : myString , 
                myArray : myArray , 
            },                 // you can insert url argumnets here to pass to api.php
            dataType: 'json',         // return data format
            success: function(data) { //
                alert(data);
             },
             error: function (jqXHR, textStatus, errorThrown){
                 console.log(textStatus, errorThrown);
             },
        });
    });
    return response;
}

api.php页面有以下内容

<?php 


    if ( isset($_POST["firstDate"]) && !empty($_POST["firstDate"])){
        $response .= "<p>firstDate= " . $_POST["firstDate"] . "</p>"; 
    }
    else $response .= " 1 ";
    if ( isset($_POST["secondDate"]) && !empty($_POST["secondDate"])){
        $response .= "<p>secondDate = " . $_POST["secondDate"] . "</p>";
    }
    else $response .= " 2 ";
    if ( isset($_POST["myString"]) && !empty($_POST["myString"])){
        $response .= "<p>myString = " . $_POST["myString"] . "</p>";
    }
    else $response .= " 3 ";
    if ( isset($_POST["myArray"]) && !empty($_POST["myArray"])){
        $response .= "<p>myArray = " . $_POST["myArray"] . "</p>";
    }
    else $response .= " 4 ";

echo json_encode($response);
?>

但是当我点击按钮时出现以下错误:

SyntaxError:JSON.parse:第 1 行第 1 列出现意外字符 JSON 数据

但是如果我将 POST 更改为 GET,我可以看到传递的变量,但仍然会出现相同的错误。

任何想法我做错了什么?

【问题讨论】:

  • SyntaxError: JSON.parse 表示服务器返回的JSON无效。
  • ALSO...为什么你的PHP中有console.log?!这肯定会引发错误。
  • 只需在浏览器开发工具的网络面板中查看对请求的实际响应 - 很可能已经告诉您哪里搞砸了。
  • 另外,您还有$_POST["myString "]。为什么那里有空间?您的密钥是 "myString",而不是 "myString "
  • 请注意,您不能在 processIT 函数中执行 return response;。 AJAX 是异步的,这意味着它在后台运行并在准备就绪时调用其回调。您不能从 AJAX 调用中返回。另外,为什么processIT 里面有$(function () {

标签: javascript php ajax forms variables


【解决方案1】:

您的 PHP 文件没有输出有效的 JSON 响应,这就是 JSON.parse 抛出错误的原因。您的 PHP 代码中有许多错误,并且这些错误包含在输出中,因此产生了无效的 JSON 响应。

console.log("firstDate" + $_POST["firstDate"]);

这不是有效的 PHP 代码。 PHP 没有console.log()。它有echo。附言你在 PHP 中使用 . 连接字符串,而不是 +.

$_POST["secondDate "]
$_POST["myString "]
$_POST["myArray "]

这些键。最后没有空间。它们应该是:

$_POST["secondDate"]
$_POST["myString"]
$_POST["myArray"]

最后,$_POST["myArray"] 是一个数组。您不能将其连接到字符串。试试这个:

$response .= "<p>myArray = ".implode(', ', $_POST["myArray"])."</p>";

【讨论】:

  • 我解决了这些问题。现在我不再收到 json 错误了。但我也没有通过变量。我确实看到它们在 fierbug 控制台中传递,但返回告诉我值是空的或未提交。
  • 如果你只做echo json_encode($_POST); die();,你看到了什么?
  • ...您看到它们在 Firebug 的请求选项卡中传递了吗?
  • 我删除了字符串化,现在我看到了我正在传递的变量。
  • JSON“通常”用于编码对象/数组(可以包含字符串、数字、对象或数组)。您可以 json_encode 一个字符串,但有时它不解码。如果它可以按您的意愿工作,那很好:-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-26
  • 2014-01-13
  • 1970-01-01
  • 2016-08-30
  • 2021-02-06
  • 2022-01-03
相关资源
最近更新 更多