簡體   English   中英

使用Angular 5將POST數據發送到PHP

[英]Sending POST data to php using angular 5

我正在嘗試從角度5到PHP調用Web服務。

我正在使用POST方法,但在PHP端檢索數據時遇到問題。

發送數據顯示在有效負載中,但未在php端檢索到。

角度服務

this.URL = "http://localhost/angular/WEBSERVICE_PHP/test.php";

const headers = new Headers();
headers.append('Content-Type', 'application/json');
let data = 'visitor_id=55';

return this.http
.post(this.URL,JSON.stringify(data),{
    headers: headers
})
.map( Response => console.log(Response) );

PHP頁面

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Methods: POST, GET");
header('P3P: CP="CAO PSA OUR"'); // Makes IE to support cookies
header('content-type: text/plain');  
header("content-type: application/x-www-form-urlencoded");
header("Access-Control-Allow-Headers: Content-Type, Authorization, X- 
Requested-With");
header("Access-Control-Max-Age: 172800");

if(isset($_POST))
{
    // $post = 'test data';     // This data get sent
    //   return $post;
    echo $post = $_POST['visitor_id'];
    return $post; 
}

“發布” JSON時,不會看到$_POST

而是這樣做:

$json = file_get_contents('php://input');
$array = json_decode($json, true);

如果必須為$_POST ,則可以將$array分配給$_POST

另一個解決方案是從Angular端發送而不是發送JSON字符串,而是發送適當的帖子! (我不是Angular的人,也許有人可以發表評論,然后我將添加代碼!)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM