簡體   English   中英

語法錯誤:JSON 中位置 0 的意外標記 I

[英]SyntaxError: Unexpected token I in JSON at position 0

我在嘗試從我制作的表中發送一些數據時遇到問題,我收到一個錯誤,它說我對數據庫所做的更新是正確的,但字段沒有更新,它向我發送了這個錯誤

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);
}

如果您能幫助我,我將不勝感激,因為我不知道我有什么問題

您必須對所有輸出(還有錯誤消息)使用 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);
    }
<?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);
    }
?>

如果您將 json 作為字符串發送,這將起作用。

JSON.stringify(ur_object 在這里)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM