如何将参数从jquery传递到restAPI

时间:2019-09-22 08:16:37

标签: json rest api

我需要一些帮助来了解如何将参数传递给Rest API以插入新记录

我写了类别的Read / Creat API,一个调用这些API的php文件 阅读API的问题,但是Creat API无法正常工作,因为我不知道如何将参数传递给Creat API

我尝试了这个https://docs.telerik.com/kendo-ui/framework/datasource/crud 但没有结果

我的用于创建类别的API

<?php
  // Headers
  header('Access-Control-Allow-Origin: *');
  header('Content-Type: application/json');
  header('Access-Control-Allow-Methods: POST');
  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/Category.php';
  // Instantiate DB & connect
  $database = new Database();
  $db = $database->connect();

  // Instantiate blog post object
  $category = new Category($db);

  // Get raw posted data
  $data = json_decode(file_get_contents("php://input"));

  $category->name = $data->name;

  // Create Category
  if($category->create()) {
    echo json_encode(
      array('message' => 'Category Created')
    );
  } else {
    echo json_encode(
      array('message' => 'Category Not Created')
    );
  }
 ?>

我的javacript是

$(document).ready(function(){
var remoteDataSource = new kendo.data.DataSource({
        autoSync: true,
        transport: {
                        read    : {
                        url     : 'http://localhost/php_rest_myblog-master/api/category/readNew.php',
                        dataType: 'json',
                        type    : 'POST'
                        },

                        create:{
                        url     : 'http://localhost/php_rest_myblog-master/api/category/Create.php',
                        dataType: 'json',
                        type    : 'POST'
                        }
                 },
         schema: {
            model:{
                id:"id",
                fields:{
                      id: { editable: false, nullable: true },
                      name: { validation: { required: true} }
                }
            }
         }

        });

   $("#grdCategory").kendoGrid({

       dataSource:remoteDataSource,
       pageable: true,
       height: 400,
       toolbar: ["create"],

       columns: [
        {field:"id", title: "ID", width: "20px"},
        {field: "name", title: "name", width: "60px"},
        { command: ["edit", "destroy"], title: "&nbsp;", width: "210px"}
       ],
       editable: "popup"
   });
});// end ready function

0 个答案:

没有答案