dsfasdfsda234234
dsfasdfsda234234
2017-11-10 06:09

React本机http获取请求无效

已采纳

I'm new to react native trying to do the http request using fetch method (also tried axios). I'm using PHP as backend. What I'm trying is to print the JSON object returned from server in react native.

I'm sure that the response is returning from server cause I printed the result and it is a JSON array. But when I consoled the responseData using Chrome dev tools I got this []. I don't know where the mistake is.

index.js

    componentDidMount(){

        fetch('http://xxxxxxxxxx/result.php', {
            method: 'GET',
        }).then(response =>response.json())
          .then((responseData) =>
          {
              console.log(responseData);
              this.setState({
                  user: (responseData.name),
                  loaded:true,
              })

        });         
}
    render(){
        return(
        <Card>  
  <CardTitle style={styles.containerStyle} title={this.state.user} />
</Card>
        );
    }
}

Also my php is script is as follows

result.php

<?php
include 'db.php';
$check_json = file_get_contents('php://input');

$obj1= json_decode($check_json);
$uid =$obj1->{'uuid'};
$blood = $obj1->{'bloodgroup'};
$loc = $obj1->{'place'};
$prep =$mysqli->prepare("select token,name,uuid from signup where location 
like '".$loc."%' and bloodgroup= ?");
$prep->bind_param("s",$blood);
$prep->execute();
$result= $prep->get_result();
$payload= array();
while($row= $result->fetch_array(MYSQL_ASSOC)){
   $payload[]= array('name' =>$row['name']
                    'uuid' =>$row['name']);
}
$obj2 =json_encode($payload);
echo $obj2;
?>

and the response from server is

 [{"name":"angel","uuid":"b4b8a266-60cf-4e30-a778-596316d1a4a2"}]
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • douzhi4991 douzhi4991 4年前

    Try using axios. You can check the docs here After you have done npm install axios and imported axios into your component you can try:

    componentDidMount(){
    axios
      .get('https://www.your-url.com/path-to-your-php-api')
      .then(({ data })=> {
        this.setState({ 
          user: data.name
        });
      })
      .catch((err)=> {})
    }
    

    if you still have problems, post the url that returns JSON so I can take a look.

    Check your PHP API, if the object is returning empty, make sure to encode json. Here is an example you can try

    $myObj->name = "Michel Arteta";
    $myObj->age = 30;
    $myObj->city = "New York";
    
    $myJSON = json_encode($myObj);
    
    echo $myJSON;
    
    点赞 评论 复制链接分享
  • duanpie2834 duanpie2834 4年前

    I've solved this.Problem was with the react part.I was trying to fetch this response returned in a different page.Actually the response returned from server was correct.

    点赞 评论 复制链接分享
  • douliwang6896 douliwang6896 4年前

    Is your url is http ? Do you try on IOS ?

    (if yes, you need add your domain to white list in XCode )

    By default, iOS will block any request that's not encrypted using SSL. If you need to fetch from a cleartext URL (one that begins with http) you will first need to add an App Transport Security exception. If you know ahead of time what domains you will need access to, it is more secure to add exceptions just for those domains;

    点赞 评论 复制链接分享

为你推荐