首先,我們使用下面的代碼,以便api只能在移動設備上運行,而不是在瀏覽器上運行。 「在swift3中通過Alamofire發出請求時出現404錯誤
$iPod = stripos($_SERVER['HTTP_USER_AGENT'], "iPod");
$iPhone = stripos($_SERVER['HTTP_USER_AGENT'], "iPhone");
$iPad = stripos($_SERVER['HTTP_USER_AGENT'], "iPad");
$Android = stripos($_SERVER['HTTP_USER_AGENT'], "Android");
$json_data = json_decode(file_get_contents('php://input'));
if ($json_data) {
$username_1 = $json_data->username;
if ($Android || $iPad || $iPhone || $iPod) {}
其次,公司正在使用AFNetworking在Android(使用凌空)和IOS Objective-C的工作。但是,現在我移植項目swift3和我使用的API‘Alamofire’網絡電話。
方法::(這是我如何做POST請求):
let headers : HTTPHeaders = [
"Accept" : "application/json"
]
Alamofire.request("API URL", method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers)
.responseJSON{
response in
if let json = response.result.value{
}
但是,我得到錯誤404,即我們通常得到,當我們在瀏覽器中點擊API 之後我刪除後端檢查和請求工作正常得到了適當的迴應。但是,我們無法刪除該支票。所以幫助我,如果我用別的標題做出任何錯誤。
這是我在debugPrint我得到(響應)::
{ status code: 200, headers {
"Access-Control-Allow-Origin" = "*";
"Cache-Control" = "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
Connection = "Keep-Alive";
"Content-Encoding" = gzip;
"Content-Length" = 84;
"Content-Type" = "text/html; charset=UTF-8";
Date = "Thu, 14 Sep 2017 17:03:17 GMT";
Expires = "Thu, 19 Nov 1981 08:52:00 GMT";
"Keep-Alive" = "timeout=5";
Pragma = "no-cache";
Server = "Apache/2.4.25";
Vary = "Accept-Encoding,User-Agent";
"X-Powered-By" = "PHP/5.6.31";
} }
[Data]: 80 bytes
[Result]: SUCCESS: {
"is_valid" = 0;
status = 404;
"status_message" = "access denied";
success = 0;
}
你確定User-Agent包含iPod,iPhone或iPad?閱讀它發送的內容:https://github.com/Alamofire/Alamofire/blob/master/README.md#http-headers。另外,由於您似乎可以控制服務器,所以作爲調試,您可以檢查用戶代理。 – Larme