无法从我的函数获取值到我的 update.php

时间:2021-05-17 16:20:14

标签: javascript php api

所以我无法从我的函数接收值来更新我的数据库,如果我手动输入值,代码就可以工作。

我的功能

   function changeStatus(id, ativo) {
        $.post(baseUrl + "article_type/update.php",
            {
                id: id,
                ativo: ativo
            },
    
            function (data) {
                console.log(data)
            });
    }

我的更新api

<?php
  // Headers
  header('Access-Control-Allow-Origin: *');
  header('Content-Type: application/json');
  header('Access-Control-Allow-Methods: PUT');
  header('Access-Control-Allow-Headers: Access-Control-Allow-Headers, Content-Type, Access-Control-Allow-Methods, Authorization,X-Requested-With');

  include_once '../../config/Database.php';
  include_once '../../models/Articles_type.php';
  // Instantiate DB & connect
  $database = new Database();
  $db = $database->connect();

$data = json_decode(file_get_contents("php://input"));
  // Set ID to UPDATE
$data = [
    'ativo' => $data->ativo,
    'id' => $data->id,
];
$sql = "UPDATE tipo_artigos SET ativo=:ativo WHERE id=:id";
$stmt= $db->prepare($sql);
$stmt->execute($data);
 
?>

1 个答案:

答案 0 :(得分:0)

下面的代码对我有用,如果你认为它是正确的格式,你可以遵循:

 header('Access-Control-Allow-Origin: *');
  //header('Content-Type: application/json');
  header('Access-Control-Allow-Methods: PUT');
  header('Access-Control-Allow-Headers: Access-Control-Allow-Headers, Content-Type, Access-Control-Allow-Methods, Authorization,X-Requested-With'); 

$data = json_decode(file_get_contents("php://input"));
  // Set ID to UPDATE
$data = [
    'ativo' => $_POST['ativo'],
    'id' => $_POST['id'],
];