简体   繁体   English

ionic $ http.post请求返回空数据

[英]ionic $http.post request return a null data

The status is fine 200 but then the data is null here is my codes: in my controller 状态很好200,但是数据为空这是我的代码:在我的控制器中

        var link = 'http://127.0.0.1/mobile/subject.php';
        var userid = localStorage.getItem('loginDetails');
        console.log(userid);

        $http.post(link, {userid}).then(function(res){
        console.log(res);
        })

in my php code. 在我的PHP代码。 require_once('connection.php'); require_once( 'connection.php');

        $post_data = file_get_contents('php://input');
        $request = json_decode($post_data);

        $userid = $request->userid;

        $subject_sql ="SELECT SubjectName FROM subjects WHERE userid = '"  

        .$userid. "' ";

        $result = $con->query($subject_sql);
        $re=$result->fetch_assoc();
        echo json_encode($re);

        ?>

Here guys i put my localstorage value in a variable "userid" because i want to use my localstorage value in my php code. 在这里,我将我的localstorage值放在一个变量“ userid”中,因为我想在我的php代码中使用我的localstorage值。 Is their something wrong with my codes specially in my controller? 他们的代码在我的控制器中有问题吗?

if your localstorage already contains the JSON object then you just need to change the {userid} to userid. 如果您的本地存储已经包含JSON对象,则只需将{userid}更改为userid。

    var link = 'http://127.0.0.1/mobile/subject.php';
    var userid = localStorage.getItem('loginDetails');
    console.log(userid);

    $http.post(link, userid, {}).success(function(res){
      console.log(res);
    })
    .error(function(err) {
      console.log(err)
    });

You can't access $request->userid because you sent { userid } as data in $http.post(). 您无法访问$ request-> userid,因为您已将{userid}作为数据发送到$ http.post()中。 Try $http.post(link, {userid : userid}). 尝试$ http.post(link,{userid:userid})。

Server side, you should : 服务器端,您应该:

  • Check if $post_data actually contains anything ( var_dump() ), 检查$ post_data是否实际包含任何内容(var_dump()),
  • Check if localStorage.getItem('loginDetails') is defined. 检查是否定义了localStorage.getItem('loginDetails')。

Advice : Do not forget to escape your sql query. 忠告:不要忘记对SQL查询进行转义。 ( How can I prevent SQL injection in PHP? ) 如何防止在PHP中进行SQL注入?

Using JSON.parse to convert it to json object it solve my problem. 使用JSON.parse将其转换为json对象可以解决我的问题。 var userid = JSON.parse(localStorage.getItem('loginDetails')); var userid = JSON.parse(localStorage.getItem('loginDetails'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM