dongqing6661 2016-08-22 17:39
浏览 60
已采纳

SyntaxError:JSON.parse:尝试inser到mysql时JSON数据的第1行第1列的意外字符

I am using new to using AJAX and PHP I am trying to send a post request to a file called insertvalue.php. I already created the table and database. I am trying to grab the value from a jquery slider through ajax and then insert that value into the mysql table. After that, I want to return the results from the mysql table.

When I try running the script I get the error "SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data". I checked, and I am not sure what I did wrong. Any assistance would be appreciated. Thanks!

db_connect.php

$connect = mysqli_connect("localhost", "private", "private", "private");
// fake credentials for posting

if (mysqli_connect_errno())
{
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$createTable = "
    CREATE TABLE IF NOT EXISTS ClassValues (
        question VARCHAR(40) NOT NULL,
        slider_value INT(50) NOT NULL
    )
";

 if ($connect->query($createTable) === TRUE) {
   //  echo "Table ClassValues created successfully";
 } else {
   echo "Error creating table: " . $connect->error;
 }

js file with ajax to send js to php

 $.ajax({
     url: 'php/insertvalue.php',
     data: { 'one': value }, // slider value 
     type: 'post',
     dataType: 'json',
     success: function(x) {
         alert(x.one);
     },
     error: function(request, status, error) {
         alert(error);
     }
 });

insertvalue.php

include 'db_connect.php';  // database connection

$one = $_POST['one'];
$array = array('one'=>$one);

echo json_encode($array);

$query = "INSERT INTO ClassValues (question, slider_value) VALUES('Question 1', $one)";
mysql_query($query);

$selection = "SELECT slider_value FROM ClassValues";
$result = $conn->query($selection);

if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        echo "slider_value" . $row["slider_value"] . "<br/>";
    }
} else {
    echo "0 results";
}
$conn->close();
  • 写回答

1条回答 默认 最新

  • dougang1605 2016-08-22 18:47
    关注

    From your JavaScript code, you are expecting JSON encoded data from the server.

    Hence, you have to make sure your server will respond with only one JSON encoded result, per each client request. The parser will not understand mixture of JSON encoded data with text like { "one": "one" }slider_value or mixture of JSON encoded data with another JSON encoded data like { "one": "one" }{ "two": "two" }.

    To achieve this, you need to convert all your echo 'some result' statements to $result = 'some result'. At the end of your script, you will then encode and echo the final result, e.g.: { "result": "Success", "message": "Data inserted successfully"}.

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧