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条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 在线教育培训平台,主要以点播视频和在线练习测试为主,除将视频做切片外,有其他哪种方式降低流量?(关键词-带宽速率)
      • ¥20 用c语音或c++实现银行叫号系统
      • ¥15 人工智能 规则正向演绎和推理
      • ¥20 基于STM32F401的电子密码锁设计
      • ¥15 famamacbeth回归中遇到only size-1 arrays can be converted to Python scalars,求解答
      • ¥15 单片机多个自锁按键的编程实践
      • ¥15 用python操作redis存储中文后,再取出的数据变成了乱码怎么办?
      • ¥15 C语言简单排序问题有偿求解
      • ¥20 请问图片的代码什么意思
      • ¥15 coq问题求带,有偿