Ank
Ank

Reputation: 178

Wordpress REST API custom form submit - React

Wanted to POST custom form data through Wordpress API using ReactJS. Like we use AJAX in Wordpress using something like

jQuery.ajax({
        type: 'POST',
        data: {
            action  : 'formData',  // wp_ajax_nopriv_formData
            data    : formData  // data from form
        },
        url: ajaxurl  // admin.php
    }).done(){...}

Wordpress and React are not on the same domain. I am using wp-apiv2.

I wanted to know how can I use WP-API to submit custom form through React (as front end) as an external call e.g.

abc.com - Wordpress domain with wp-api installed xyz.com - ReactJS implemented website - from here I will call all the API's endpoints

Upvotes: 0

Views: 1710

Answers (1)

jmunsch
jmunsch

Reputation: 24099

Using jsx what about:

<form onsubmit={()=>{
    jQuery.ajax({
        type: 'POST',
        data: {
            action  : 'formData',  // wp_ajax_nopriv_formData
            data    : formData  // data from form
        },
        url: ajaxurl  // admin.php
    }).done(){...}
}}>
<input type='text' value='hello'></input>
<button type="submit">submit</button>
</form>

Or with just React:

'use strict';

React.createElement(
    'form',
    { onsubmit: function onsubmit() {
            jQuery.ajax({
                type: 'POST'
            });
        } },
    React.createElement('input', { type: 'text', value: 'hello' }),
    React.createElement(
        'button',
        { type: 'submit' },
        'submit'
    )
);

Upvotes: 1

Related Questions