doufei5537 2016-12-20 15:56
浏览 240
已采纳

页面加载时的Ajax $ .post - React

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?

  • 写回答

1条回答 默认 最新

  • douyin2962 2016-12-23 11:09
    关注

    I have solved the problem by doing the following:

    A solution for the code above doesn't exist. What I did is simply not passing the publisher_id to React at all. I simply opened the php session in my api and used the value saved is the $_SESSION variable.

    After doing this, everything works the way I want it to.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 手机连接电脑热点显示无ip分配
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大