如何访问php服务器上的请求负载

时间:2017-10-09 13:00:41

标签: php json ionic-framework

我正在尝试将数据从离子发送到php服务器。但是请求,发布和获取都是空的。我试过邮差,它的工作原理,但如果我发送与http.post的离子不起作用。我尝试在php上使用:

$data = file_get_contents("php://input");
$data = json_decode($data, TRUE);

我有这个:

if (isset($_SERVER['HTTP_ORIGIN'])) {
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400');    // cache for 1 day
}

// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {

if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
    header("Access-Control-Allow-Methods: GET, POST, OPTIONS");         

if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
    header("Access-Control-Allow-Headers:        {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

exit(0);
}
$data = file_get_contents("php://input");
$data = json_decode($data, TRUE);

echo $data;

$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
$username = htmlspecialchars($username, ENT_QUOTES);
$password = md5($password . PASSWORD_DEFAULT2);

但数据为空。在离子中我做:

let credentials = {username:user,password:pass};
let headers = new Headers();
this.http.post(this.serverUrl+'login.php',JSON.stringify(credentials),{headers: headers})
  .map(response =>{  return response.json()})
  .subscribe(data => console.log(data))
  ,error => console.log("loading error: ");   

在chrome的开发者工具中,我可以看到想要发送的数据。

0 个答案:

没有答案