0

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.

  • Are there any working examples regarding this? (React or Angular)
  • How can I use OAuth to get to admin.php in Wordpress to submit my form?

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

Ank
  • 168
  • 1
  • 2
  • 11

1 Answers1

1

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'
    )
);
jmunsch
  • 22,771
  • 11
  • 93
  • 114
  • Hi thanks for the reply but I think you got the wrong question, 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 – Ank May 30 '16 at 16:12
  • @Ank I see. You can set the server side cors headers to whitelist multiple domains like: http://stackoverflow.com/questions/1653308/access-control-allow-origin-multiple-origin-domains also double check the w3c specs for more depth. – jmunsch May 30 '16 at 16:45