【问题标题】:SyntaxError: Unexpected token I in JSON at position 0SyntaxError:位置 0 处 JSON 中的意外标记 I
【发布时间】:2020-02-10 18:31:11
【问题描述】:

我在尝试从我创建的表中发送一些数据时遇到问题,我收到一个错误,它说我对数据库所做的更新是正确的,但字段没有更新,它向我发送了这个错误

parsererror
SyntaxError: Unexpected token I in JSON at position 0
    at JSON.parse (<anonymous>)
    at n.parseJSON (jquery.min.js:4)
    at vc (jquery.min.js:4)
    at x (jquery.min.js:4)
    at XMLHttpRequest.<anonymous> (jquery.min.js:4)

我真的不知道这个错误是什么,因为当我点击发送按钮时,我收到了这个错误并且字段没有更新......这是我的脚本

 <script>
  function viewData(){
    $.ajax({
      url: '../A30.php?p=view',
      method: 'GET',
    }).done(function(data){
      $('tbody').html(data)
      tableData()
    })
  }
  function tableData(){
    $('#tabledit').Tabledit({
      url:'../A30.php',
      eventType: 'dblclick',
      editButton : true, 
      deleteButton : false,
       columns:{
              identifier:[0,'ID'],
              editable: [[0,'ID'],[1, 'Empleado'],[5,'Monto']]
      },
      buttons:{
      style: 'width:150px;',
        edit:{
          class: 'btn btn-sm btn-success' ,
          html: '<span class="fa fa-pencil-square-o" ></span> Editar',
          action: 'edit'
        },
        delete:{
          class: 'btn btn-sm btn-default',
          html: '<span class="glyphicon glyphicon-trash"></span> Trash',
          action: 'delete'
        },
        save:{
          class: 'btn btn-sm btn-info',
          html: '<span class="fa fa-floppy-o  "></span> Guardar'
        },
        restore:{
          class: 'btn btn-sm btn-warning',
          html: 'Restore',
          action: 'restore'
        },
        confirm:{
          class: 'btn btn-sm btn-danger',
          html: 'Confirm'
        },
      },
      onSuccess: function(data, textStatus, jqXHR){
        viewData()
      },
      onFail: function(jqXHR, textStatus, errorThrow){
        console.log('onFail(jqXHR, textStatus, errorThrow)');
        console.log(jqXHR);
        console.log(textStatus);
        console.log(errorThrow);
      },
      onAjax: function(action, serialize){
        console.log ('onAjax(action, serialize)');
        console.log(action);
        console.log(serialize);
      }
    });
  }
  </script>

这里我有 php 中的代码,我在其中接收变量以将其发送到我的数据库但它没有发送它只告诉我它已成功插入但在数据库中它们没有更新

header('Content-Type: application/json');
    $input = filter_input_array(INPUT_POST);

    if ($input['action'] === 'edit') {
            $actualizar ="UPDATE sysnom_nomina_det_tmp SET monto='".$input['Monto']."' where ID='".$input['ID']."' AND empleado = '".$input['Empleado']."' AND monto = '".$input['Monto']."' ";
            $resul = sqlsrv_query($conexion, $actualizar);
              if(!$resul){
                echo "Error al Actualizar";
                echo $actualizar;
                echo $resul;
              } else {
                echo "Insertado exitosamente......";
                echo $resul;
              }

        }
  sqlsrv_close($resul);
    echo json_encode($input);
}

如果您能帮助我,我将不胜感激,因为我不知道自己有什么问题

【问题讨论】:

  • 你真的需要了解准备好的/参数化的语句。这对注射敞开大门
  • 但是我该如何解决它

标签: javascript php sql-server


【解决方案1】:
<?php
    require ('recursos/conexion.php');



    $page = isset($_GET['p'])? $_GET['p'] : ''
;   if ($page=='view') {

        $sql = "exec sp_sys_nom_autoriza2 'A30',1";
        $stmt = sqlsrv_query($conexion, $sql);
      $select =  "select * from sysnom_nomina_det_tmp where suc = 'A30'";
      $stm = sqlsrv_query($conexion, $select);
        $totalVU = sqlsrv_num_rows($stm);     
       while($row = sqlsrv_fetch_array($stm, SQLSRV_FETCH_ASSOC)) {

         if ($row['notas'] !== "AHORRO" ) {
              if ($row['notas'] !== "PRESTAMO DE AHORRO") {
         ?>
        <tr  style="width:1900px" >

          <td style="width:141px;"><?php echo $row["ID"]?>     </td>
          <td style="width:141px;"><?php echo $row["empleado"]?></td>
          <td style="width:141px;"><?php echo $row["nombre"]?>  </td>
          <td style="width:141px;"><?php echo $row["depto"]?>   </td>
          <td style="width:141px;"><?php echo $row["percepcion"]?></td>
          <td style="width:141px;"><?php echo $row["monto"]?></td>
          <td style="width:141px;"><?php echo $row["notas"]?>   </td>
<?php
          $autorizar = rtrim($row['autorizar']);
           if ($autorizar == 1) {
                      echo "<td id='autorizar'><input type ='checkbox' name='checkbox' id='checkbox' value='1' style='width:30px; height:30px;' checked></td>";

                      }else{
                         echo "<td id='autorizar'><input type ='checkbox' name='checkbox' id='checkbox' value='1' style='width:30px; height:30px;'></td>";
                      }

    }  
  }
}
}else{

  header('Content-Type: application/json');
    $input = filter_input_array(INPUT_POST);
    $respone = [
        "message"=>"",
        "data"=>[]
    ];
    if ($input['action'] === 'edit') {
        $actualizar ="UPDATE sysnom_nomina_det_tmp SET monto='".$input['Monto']."' where ID='".$input['ID']."' AND empleado = '".$input['Empleado']."' AND monto = '".$input['Monto']."' ";
        // you can use prepeared statetments for sql injection threats
        $resul = sqlsrv_query($conexion, $actualizar);
        if(!$resul)                                        
            $respone["message"] = "Error al Actualizar";
        else
            $respone["message"] = "Insertado exitosamente......";
    }
    sqlsrv_close($resul);
    $response["data"]["query"] = $actualizar;
    $response["data"]["result"] = $resul;
    $response["data"]["input"] = $input;
    echo json_encode($response);
    }
?>

【讨论】:

    【解决方案2】:

    您必须对所有输出(以及错误消息)使用 json_encode 函数。 试试这个版本:

        header('Content-Type: application/json');
        $input = filter_input_array(INPUT_POST);
        $respone = [
            "message"=>"",
            "data"=>[]
        ];
        if ($input['action'] === 'edit') {
            $actualizar ="UPDATE sysnom_nomina_det_tmp SET monto='".$input['Monto']."' where ID='".$input['ID']."' AND empleado = '".$input['Empleado']."' AND monto = '".$input['Monto']."' ";
            // you can use prepeared statetments for sql injection threats
            $resul = sqlsrv_query($conexion, $actualizar);
            $response["data"]["result"] = $resul;
            if(!$resul)                                        
                $respone["message"] = "Error al Actualizar";
            else
                $respone["message"] = "Insertado exitosamente......";
        }
        sqlsrv_close($resul);
        $response["data"]["query"] = $actualizar;        
        $response["data"]["input"] = $input;
        echo json_encode($response);
        }
    

    【讨论】:

    • 我在哪里可以使用它来知道我的错误在哪里
    • 如果您期望 json 响应,您必须将它用于所有输出(回声)。实际上,您可以检查 devtools 并观察网络以获取响应。这是接受来自后端的响应的 js 错误。我添加了截图:ibb.co/Kw4NHGm
    • 在我看来不是这样
    • A30.php 点击它并查看响应。
    • 使用您传递给我的代码时会出错
    【解决方案3】:

    如果您将 json 作为字符串发送,这将起作用。使用

    JSON.stringify(此处为 ur_object)

    【讨论】:

      猜你喜欢
      • 2020-07-02
      • 2021-05-06
      • 1970-01-01
      • 2017-12-24
      • 2017-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-13
      相关资源
      最近更新 更多