简体   繁体   English

方法POST的Wordpress REST API自定义端点

[英]Wordpress REST API custom endpoint for method POST

I am currently working on a project which requires a WordPress website and a simple REST api. 我目前正在开发一个需要WordPress网站和简单REST API的项目。 I discovered that WordPress has its own REST api and decided to extend its functionality to meet my needs. 我发现WordPress有自己的REST api,并决定扩展其功能以满足我的需求。 All I need to do is to have endpoints for GET and POST requests that retrieve/insert data from/to a table which is not directly related to WordPress (but in the same database). 我需要做的就是为GET和POST请求提供端点,这些端点从/向与WordPress没有直接关系的表(但在同一个数据库中)检索/插入数据。 I successfully implemented all GET requests, however, I am struggling to get the POST ones working. 我成功地实现了所有GET请求,但是,我正在努力让POST工作正常。
I have this route register defined: 我有这个路由寄存器定义:

register_rest_route('api/v1', 'create-player/', array(
        'methods' => 'POST',
        'callback' => 'create_player',
));

The client sends a request through ajax call which is expected to hit the endpoint from the route above. 客户端通过ajax调用发送请求,该调用有望从上面的路由到达端点。 This is the ajax: 这是ajax:

    $.ajax({
       method: "POST",
       url: '/wp-json/api/v1/create-player/',
       data : JSON.stringify(data),
       contentType: 'applcation/json',
       beforeSend: function (xhr){
           xhr.setRequestHeader("X-WP-None", locData.nonce);
           console.log('beforeSend');
       },
       success: function(response){
           console.log("success" + response);
       },
       fail: function (response){
           console.log("fail" + response);
       }
    });

I am not sure how to build the POST route register from the REST api, the other GET requests have an attribute args that map directly to the parameters passed in the endpoint. 我不确定如何从REST api构建POST路由寄存器,其他GET请求具有直接映射到端点中传递的参数的属性args Do I need something like that to handle the request data when using POST? 使用POST时,我是否需要类似的东西来处理请求数据? How do I get the data type passed from the ajax and then use it in my function create_player(); 如何获取从ajax传递的数据类型,然后在我的函数create_player();使用它create_player(); The WP REST API documentation seems to be incomplete and all of the information I found uses endpoints for built-in WordPress features such as posts/authors/blogs etc. but I don't need that, I just want to use the provided functionality and create my own interface. WP REST API文档似乎不完整,我发现的所有信息都使用内置WordPress功能的端点,如帖子/作者/博客等。但我不需要,我只想使用提供的功能和创建我自己的界面。 Thank you. 谢谢。

in your callback function you can use something like this: 在你的回调函数中你可以使用这样的东西:

 $param = $request->get_param( 'some_param' );

  // You can get the combined, merged set of parameters:
 $parameters = $request->get_params();

https://www.coditty.com/code/wordpress-api-custom-route-access-post-parameters https://www.coditty.com/code/wordpress-api-custom-route-access-post-parameters

Finally found it! 终于找到了! In order to access the body of the POST request use $request->get_body(); 为了访问POST请求的主体,使用$request->get_body(); in your register_rest_route callback method. register_rest_route回调方法中。

Add POST in methods while registering route and in your callback function access your POST variables via $request array. 在注册路由时在方法中添加POST,在回调函数中通过$request数组访问POST变量。 That's it. 而已。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM