2016-04-23 58 views
0

當我收到來自AlamoFire的請求時,PHP中的腳本無法識別POST,GET或REQUEST變量中的參數。PHP Wamp服務器沒有從AlamoFire請求接收參數

在AlamoFire的代碼是

let parameters: [String: AnyObject] = 
     ["email" : email, 
     "password": password] 

    Alamofire.request(.POST, url, parameters: parameters, encoding: .JSON) 
     .responseJSON { response in 

      print(response) 

      switch response.result { 

      case .Success: // Connection stablished 



      case .Failure(let error): // Connection not stablished 
       print(error) 
       self.showLoginError(Constants.connectionError) 
      } 
    } 

而在PHP的代碼是:

$response = array(); 
header('Content-Type: application/json'); 
if (isset($_POST['email']) && isset($_POST['password'])) { 
    ... stuff 
}else{ 
    $response["Failure"] = 1; 
    $response["message"] = "Required field(s) is missing"; 
    echo json_encode($response); 
} 

然後響應於AlamoFire總是與所需字段

的消息中的JSON在一個點PHP給了我這個錯誤: PHP已棄用:自動填充$ HTTP_RAW_POST_DATA已被棄用,將被重新在未來的版本中移動。爲避免此警告,在php.ini中將'always_populate_raw_post_data'設置爲'-1',並改爲使用php://輸入流。在未知在線0

我不得不改變的php.ini與選項always_populate_raw_post_data爲-1,達到了我什麼,我現在的狀態。

但是,如果請求的URL是這樣的http://192.168.1.108:8080/labor/[email protected]&password=brown1234服務器接收參數。另外,如果我使用NSURLSession創建請求,並將參數作爲HTTP Body的一部分,服務器將獲取它們。

回答

0

你怎麼知道它沒有收到它?你怎麼知道alomfire是否發送它? 使用'回聲'看看你的PHP腳本alomfire帶回。如果確實是json值,則記住您必須解碼json值,然後才能使用或分配它們。 How to decode json

$mymail= ($_POST['email']); 
$mypass= ($_POST['password']); 
echo $mymail; 
echo $mypass; 

//Decode your json first here 

if ($mymail && $mypass) { 
    ... stuff 
}else{ 
$response["Failure"] = 1; 
$response["message"] = "Required field(s) is missing"; 
echo json_encode($response); 

}

相關問題