dsdtszi0520538 2016-05-07 19:57 采纳率: 100%
浏览 53
已采纳

我无法通过AJAX将会话存储数据发送到服务器

I have stored the ID of the user in session storage. I need to perform an action using the ID so I am passing it to the server using AJAX but the data is not reaching the server.

AJAX:-

$(document).ready(function(){
  $('#coupon_code').hide();
  $('#coupon_status').hide();
  var id = sessionStorage.getItem('user_id');

  $('#generate').click(function(){
    $.ajax({
      type : "POST",
      dataType : 'json',
      url : 'http://127.0.0.1/ionic/generate.php',
      data : id,
      beforeSend : function(){
        console.log(sessionStorage.getItem('user_id'));
        console.log("data sent");
      },
      success : function(data){
        console.log(data);
        console.log(data.code);
        console.log(data.value);
      }
    });
  });
});

PHP script:-

<?php
require 'db_connect.php';
header('Access-Control-Allow-Origin: *');
$id = $_POST['id'];

$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$string = '';

for ($i = 0; $i < 5; $i++) {
    $string .= $characters[mt_rand(0, strlen($characters) - 1)];
}
$ins = "INSERT INTO `coupon_code` (id,code,status) VALUES ('$id,'$string',1)";
$res_ins = mysqli_query($con,$ins);

if($res_ins)
{
    echo json_encode(array("status" => "done", "code" => $string,"value" => "1"));
    //echo $string;
}
else
{
    echo "NO";
}
?>

EDIT :- Firebug result enter image description here

  • 写回答

1条回答 默认 最新

  • dongyongkui6329 2016-05-07 20:19
    关注

    You are only sending value in json data - you need to send key-value pair - so through key you will get data at server

    This should work for you - also you can debug data in firebug what is going to server or not

    You need to replace This line data : id,

    with this line data : {'id' : id},

     $(document).ready(function(){
          $('#coupon_code').hide();
          $('#coupon_status').hide();
          var id = sessionStorage.getItem('user_id');
    
          $('#generate').click(function(){
            $.ajax({
              type : "POST",
              dataType : 'json',
              url : 'http://127.0.0.1/ionic/generate.php',
              data : {'id' : id},
              beforeSend : function(){
                console.log(sessionStorage.getItem('user_id'));
                console.log("data sent");
              },
              success : function(data){
                console.log(data);
                console.log(data.code);
                console.log(data.value);
              }
            });
          });
        });
    

    There is also a bug in insert query -

    $ins = "INSERT INTO `coupon_code` (id,code,status) VALUES ('$id,'$string',1)";
    

    (')single quote not closed for '$id

    $ins = "INSERT INTO `coupon_code` (id,code,status) VALUES ('$id','$string',1)";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch