douyan3478 2017-03-08 09:56
浏览 33
已采纳

AJAX POST到PHP无法正常工作

I am trying to POST a JSON to PHP where it should be decoded and pushed to MySQL database.

This is my JavaScript code.

    var dictstring = "{sensorid: \""+ name +"\", x: " +pos.x +" ,y: "+pos.y+"}";
                    console.log(dictstring);

                    $.ajax({
                        type: "POST",
                        contentType: 'application/json; charset=utf-8',
                         dataType: "json",
                        url: "myfile.php",
                        data: JSON.stringify(dictstring),
                        success: function(data){
                        alert('Success');
                        },
                    error: function(e){

                    console.log(e.message);
                        }
                    });

And this is my PHP code

<?php


$jsonData = file_get_contents('php://input');

$data_back = json_decode($jsonData);
$unit = $data_back->{"sensorid"};
$x_axis = $data_back->{"x"};
$y_axis = $data_back->{"y"};


// Create connection
$con=mysqli_connect("xxxxxxx:xxxx","xxxxx","xxxx","xxxxxxxx");

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

//Insert Values
$sql_hm = "INSERT INTO xxxx(unit, x_axis, y_axis)
VALUES ('$unit', $x_axis, $y_axis)";

if ($con->query($sql_hm) === TRUE) {
    echo "values inserted successfully"; 

} else {
     echo "No data ";
}



?>

I know the PHP part works - I tried POSTing JSON data with contentType: application/json from a REST client app and it works. But when I am trying it with my Javascript code, it isnt POSTing. I can see the "dictstring" string value in console. I cannot see the "Success" alert too. Interestingly, "Success" alert is shown if I remove the dataType: "json" line. But, POSTing is still not happening as I cannot see the values in database.

What am I missing here?

  • 写回答

4条回答 默认 最新

  • doufangxie0203 2017-03-08 10:03
    关注
    var dictstring = "{sensorid: \""+ name +"\", x: " +pos.x +" ,y: "+pos.y+"}";
    

    You have a string.

    data: JSON.stringify(dictstring),
    

    Which you then convert to a JSON representation of that string.

    $data_back = json_decode($jsonData);
    

    Which you decode to a string.

    $unit = $data_back->{"sensorid"};
    

    Which you try to treat like an object.


    Start with an object, not a string:

    var dictstring = { sensorid: name, x: pos.x, y: pos.y };
    

    … you probably want a different variable name too.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?