I want to post data via $.post
in ReactJS and use it in an api made to retrieve data from a database with some criteria like publisher_id.
What I am trying to do is take the publisher_id from a special class called User and then post it via my React app into the api files. I stumbled upon a problem which is I can only use $.post when sending data to the server via a form with, usually, a onSave
function. I am fully aware of how that works but my question is:
Is there a way to send data with $.post
or anything right when the rendering process starts?
I have tried using componentWillMount
for that matter but I stumbled upon another issue. I have noticed that usually, for $.post
to work, an event must be triggered.
What kind of event should I use to do what I want?
Code
For the $.get
method I use the following which works just fine:
componentDidMount: function ()
{
this.serverRequest = $.get("get_user_id.php", function (publisher_id)
{
this.setState(
{
publisher_id: JSON.parse(publisher_id)
});
}.bind(this));
}
What I need to do for my api to work is right after this retrieving of data happens, to post publisher_id
into read.php
, file responsable for retrieving only specific data from the database.
EDIT:
What I have tried doing is the following:
componentDidMount: function ()
{ //previous code
this.serverRequest = $.post("read_all_products.php", {
publisher_id: this.state.publisher_id,
})
}
As mentioned before, the retrieve of data fully works since when later in the render function trying to display it with {this.state.publisher_id} it will correctly show up. What doesn't work is the post of data. I may not be fully aware of how I am supposed to do it since , again, I am trying to send it on page load.
EDIT2: I have also tried passing publisher_id as a prop from the main component, so the $.get method now happens there. Then I did the following:
componentWillMount: function () {
this.serverRequest = $.post("read_all_products.php", {
publisher_id: this.props.publisher_id,
});
},
componentDidMount: function () {
this.serverRequest = $.get("read_all_products.php", function (products) {
this.setState({
products: JSON.parse(products)
});
}.bind(this));
}
That didn't work and I tried sending a fake form using an onSave function:
onSave: function (e) {
$.post("read_all_products.php", {
publisher_id: this.props.publisher_id,
});
e.preventDefault();
}// used after this in the render method on a form with a button, both
//have onSubmit and onClick set to {this.onSave}
I still look forward to somebody's help. I am pretty much stuck and have been for the past few days. I need to get this to work or I either need to change the authentication or keep being stuck on this.
The question is: How do I use $.post , in React, to send a prop called publisher_id to my .php api file on page load or even, say if anything else isn't possible, by making a button that sends the data with an empty form that presses itself once before the componentDidMount, probably in componentWillMount?