dsfasdfsda234234
2017-11-10 06:09
浏览 235
已采纳

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"}]

图片转代码服务由CSDN问答提供 功能建议

我是新手,尝试使用fetch方法(也尝试过axios)来尝试执行http请求。 我正在使用PHP作为后端。 我正在尝试打印从本机响应的服务器返回的JSON对象。

我确定响应从服务器返回,因为我打印了结果,它是一个JSON 阵列。 但是当我使用Chrome开发工具安装 responseData 时,我得到了这个 [] 。 我不知道错误在哪里。

index.js

  componentDidMount(){\  n 
 fetch('http://xxxxxxxxxx/result.php',{
 method:'GET',
})。然后(response =&gt; response.json())
 .then((responseData  )=&gt; 
 {
 console.log(responseData); 
 this.setState({
 user:(responseData.name),
 loaded:true,
})
 
});  
} 
 render(){
 return(
&lt; Card&gt; 
&lt; CardTitle style = {styles.containerStyle} title = {this.state.user} /&gt; 
&lt; / Card&gt; \  n); 
} 
} 
   
 
 

此外我的php脚本如下

结果。 php

 &lt;?php 
include'db.php'; 
 $ check_json = file_get_contents('php:// input'); 
 \  n $ obj1 = json_decode($ check_json); 
 $ uid = $ obj1-&gt; {'uuid'}; 
 $ blood = $ obj1-&gt; {'bloodgroup'}; 
 $ loc = $ obj1-  &gt; {'place'}; 
 $ prep = $ mysqli-&gt; prepare(“select sign,name,uuid from signup where location 
like'”。$ loc。“%'and bloodgroup =?”); \  n $ prep-&gt; bind_param(“s”,$ blood); 
 $ prep-&gt; execute(); 
 $ result = $ prep-&gt; get_result(); 
 $ payload = array();  
while($ row = $ result-&gt; fetch_array(MYSQL_ASSOC)){
 $ payload [] = array('name'=&gt; $ row ['name'] 
'uuid'=&gt; $ row [  'name']); 
} 
 $ obj2 = json_encode($ payload); 
echo $ obj2; 
?&gt; 
   
 
 

和响应 来自服务器是

  [{“na 我“:”天使“,”uuid“:”b4b8a266-60cf-4e30-a778-596316d1a4a2“}] 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douzhi4991 2017-11-10 06:29
    已采纳

    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;
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douliwang6896 2017-11-10 12:34

    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;

    评论
    解决 无用
    打赏 举报
  • duanpie2834 2017-11-11 05:48

    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.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题