意外的记号“A”记号、意外

由网友(若即若离)分享简介:使用jQuery我已经做了以下JSON数据:Using jQuery I have made the following JSON data: [{"name":"date","value":"24-05-2013"},{"name":"omschrijving","value":""}]这是所有有效的JSON,但...

使用jQuery我已经做了以下JSON数据:

Using jQuery I have made the following JSON data:

[{"name":"date","value":"24-05-2013"},{"name":"omschrijving","value":""}]

这是所有有效的JSON,但是当我尝试使用jQuery它给我下面的错误引发的数据:

This is all valid JSON, but when I try to fire the data using jQuery it is giving me the following error:

意外标记A

在这里,您可以看到AJAX调用:

Here you can see the AJAX call:

$.ajax({
        type: "POST",
        url: "modules/rma/ajaxhandler.php",
        contentType:"application/json; charset=utf-8",
        data: goededata,
        dataType: 'json',
        succes: function(data) { alert(data); },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert( textStatus + " " + errorThrown); }
    }).done(function() {
    });

ajaxhandler.php包含以下行:

ajaxhandler.php contains the following lines:

<?php
error_reporting(E_ALL);
session_start();

/**
 * Ajaxhandler
 *
 */

print_r($_POST);

echo json_decode($_POST['data']);
?>

这需要发送由以下方式中的数据:

The data that needs to be sent is made the following way:

var allFields = $( [] ).add( date ).add( omschrijving ).add( klachtomschrijving ).add(status).add(artikelnummer).add(klantid).add(meldidrepro).add(meldidaankoop).add(leverancier).add(inkoopregelid).add(serienummer);`

var goededata = JSON.stringify(allFields.serializeArray());

我该如何纠正这个错误?

How can I correct this error?

推荐答案

您无法使用的print_r ,因为您请求的JSON。服务器响应无效。注释掉的print_r 呼叫,它应该工作。

You can not use print_r because you are requesting json. The Server-Response is not valid. Comment out the print_r call and it should work.

在意外的标记A来自输出的print_r

The unexpected token 'a' comes from the output of print_r:

array(
  ...
)

您可以使用一个额外的按键进行调试:

You could use an extra key for debugging:

echo json_decode(array(
  'data' => $_POST['data'],
  'debug' => print_r($_POST, true), // note the second parameter for print_r
));

在您使用 response.data 和调试输出是'response.debug客户端。

on the client side you work with response.data and your debug output is in `response.debug'.

但是,为什么不只需登录服务器端的调试输出到一个文件?

But, why not simply log debug output on the server side into a file?

使用error_reporting(E_ALL); 将是一个问题太

它总是一个好主意,设置响应类型:

It's always a good idea to set the response type:

header('Content-type: application/json');