无法对类型为object的对象进行非规范化,找不到支持的规范化器

时间:2019-08-16 14:01:58

标签: drupal-8

我创建了自定义的休息资源,它对“ GET”的工作正常  方法,当我使用“ POST”方法和“令牌”发送数据以插入或更新自定义表中的数据时,我遇到错误

  

错误:“无法对类型的对象进行非规范化,不支持   找到规范化器。”

我已经按照下面的链接创建了休息源,就像链接中一样

BadRequestHttpException POST REST Resource

自定义休息资源ExampleResponse.php

namespace Drupal\Example\Plugin\rest\resource;

use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ResourceResponse;

/**
 * Provides an Example Resource
 *
 * @RestResource(
 *   id = "example_resource",
 *   label = @Translation("Example Resource"),
 *   uri_paths = {
 *     "canonical" = "/api/basic",
 *     "https://www.drupal.org/link-relations/create" = "/api/basic"
 *   }
 * )
 */

class ExampleResource extends ResourceBase {
    /**
    * Responds to GET requests.
    * @return \Drupal\rest\ResourceResponse
    */
    public function get() {
        $response = ['data' => 'Basic Authentication'];
        return new ResourceResponse($response);
    }

    /**
    * Responds to POST requests.
    * @return \Drupal\rest\ResourceResponse
    */
    public function post($data) {
        $response = ['data' => $data];
        return new ResourceResponse($response);
    }
}

向定制休息服务的jquery请求

var data = JSON.stringify({
    message: 'Hello World',
   });

   var settings = {
   "async": true,
   "crossDomain": true,
   "url": "/api/basic?_format=json",
   "method": "POST",
   "headers": {
    "x-csrf-token": token,
    "authorization": "Basic YWRtaW46YWRtaW4=",
    "cache-control": "no-cache",
    "Content-Type": "application/json",
    "Accept": 'application/json',
   },
   "data": data,
   "dataType": "JSON",
   }

  $.ajax(settings).done(function (response) {
   console.log(response);
  });

预期的{“ message”:“ Hello World”}

  

实际输出:{错误:“无法对类型的对象进行非规范化,否   找到支持规范化器。“}

0 个答案:

没有答案