【问题标题】:Ajax Error “SyntaxError: JSON.parse: unexpected character”Ajax 错误“SyntaxError:JSON.parse:意外字符”
【发布时间】:2013-08-14 13:18:24
【问题描述】:

我正在使用带有 ajax 的 jquery。我可以成功传递数据,但我收到“SyntaxError: JSON.parse: unexpected character”的错误

这是我的代码:

$.ajax({
    method:'post',
    dataType:'json',
    url: "storeEventData.php",
    data: $('#recuringForm').serialize(),
    onSuccess: function(response){
         alert("hello");
    },
        error: function(jqXHR, textStatus, errorThrown) {
           console.log(errorThrown);
        }

});

这里是 storeEventData.php 代码:

<?php 

ob_start();

// Start the session
session_start();

$_SESSION = $_POST;

echo '<pre>'; print_r($_SESSION); exit;
?>

让我知道我哪里出错了?

谢谢

【问题讨论】:

  • 从 storeEventData.php 粘贴代码
  • @hex4,我添加了 storeEventData.php

标签: json jquery


【解决方案1】:

1) 我认为您实际上不能将 $_POST 分配给 $_SESSION 变量。你需要做类似$_SESSION['post'] = $_POST

2) 你有datatype: 'json',因此你需要使用json_encode从php脚本中回显

【讨论】:

  • 我已经按照您的建议进行了尝试。语法错误已解决,但我没有收到成功事件。
  • 不是onSuccess,只是success
  • 哦,是的,那是我的错误。谢谢你帮助我。
【解决方案2】:

只需将代码替换如下

替换:

onSuccess: function(response){
     alert("hello");
}  

收件人:

success: function(response){
     alert("hello");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-27
    • 1970-01-01
    相关资源
    最近更新 更多