N
N
Ninja Mate2016-02-09 15:05:16
JavaScript
Ninja Mate, 2016-02-09 15:05:16

How to send variables to jQuery for AJAX (ReactJS application)?

The problem is the following - I don’t understand how to set a variable instead of a static string for ajax

getFormData() {
        var data = {
            name: this.refs.name.getDOMNode().value,
            mail: this.refs.mail.getDOMNode().value,
            text: this.refs.text.getDOMNode().value
        };

        return data;
    },

   handleSubmit(data=this.getFormData()) {

       jQuery.post(
           MyAjax.ajaxurl,
           {
               'action': 'send_message',
               'name': data.name,
               'email': data.mail,
               'message': data.text
           },
           function(response){
               alert('The server responded: ' + response);
           }
       );
    },

Variables 'name': 'email': 'message': tried data.name, `${data.name}`, String(data.name) and more... But nothing works. How is this solved?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita Gushchin, 2016-02-09
@victorzadorozhnyy

Mistakes?
Check what this refers to. And the function call in the default parameters looks out of place)
handleSubmit(data=this.getFormData()) {

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question