Php 在Alamofire请求主体中发送JSON数据

Php 在Alamofire请求主体中发送JSON数据,php,json,api,swift3,alamofire,Php,Json,Api,Swift3,Alamofire,我正在尝试使用Alamofire(版本4.0)进行api调用以提交一些数据。我认为这个请求已经成功了。但我遇到的问题是,当我打电话时,我从服务器得到一个0字节故障的响应 我在StackOverflow上尝试了许多当前的解决方案,但找不到解决方案。谢谢你的帮助 这是我的邮差设置: import UIKit import Alamofire class ViewController: UIViewController { override func viewDidLoad() {

我正在尝试使用
Alamofire
(版本4.0)进行api调用以提交一些数据。我认为这个请求已经成功了。但我遇到的问题是,当我打电话时,我从服务器得到一个0字节故障的响应

我在StackOverflow上尝试了许多当前的解决方案,但找不到解决方案。谢谢你的帮助

这是我的
邮差
设置:

import UIKit
import Alamofire

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let headers: HTTPHeaders = [ "content-type": "x-www-form-urlencoded"]


        Alamofire.request("myurl.php?method=GET_RECENT", headers: headers).responseJSON { response in
            debugPrint(response)
            print(response.request)  // original URL request
            print(response.response) // HTTP URL response
            print(response.data)     // server data
            print(response.result)   // result of response serialization

            if let JSON = response.result.value {
                print("JSON: \(JSON)")
            }
        }

    }
}

我的Swift代码:

import UIKit
import Alamofire

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let headers: HTTPHeaders = [ "content-type": "x-www-form-urlencoded"]


        Alamofire.request("myurl.php?method=GET_RECENT", headers: headers).responseJSON { response in
            debugPrint(response)
            print(response.request)  // original URL request
            print(response.response) // HTTP URL response
            print(response.data)     // server data
            print(response.result)   // result of response serialization

            if let JSON = response.result.value {
                print("JSON: \(JSON)")
            }
        }

    }
}
我得到的错误是:

import UIKit
import Alamofire

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let headers: HTTPHeaders = [ "content-type": "x-www-form-urlencoded"]


        Alamofire.request("myurl.php?method=GET_RECENT", headers: headers).responseJSON { response in
            debugPrint(response)
            print(response.request)  // original URL request
            print(response.response) // HTTP URL response
            print(response.data)     // server data
            print(response.result)   // result of response serialization

            if let JSON = response.result.value {
                print("JSON: \(JSON)")
            }
        }

    }
}
[请求]:myurl.php?method=GET\u RECENT [响应]:{URL:myurl.php?method=GET_RECENT}{status code:200,headers{ “缓存控制”=“最大年龄=0”; 连接=“保持活动”; “内容长度”=0; “内容类型”=“文本/html;字符集=UTF-8”; 日期=“2017年1月1日星期日11:41:31 GMT”; Expires=“2017年1月1日星期日11:41:31 GMT”; “保持活动”=“超时=7,最大=100”; Server=“Apache/2.2.15(CentOS)”; “X-Powered-By”=“PHP/5.6.25”; } } [数据]:0字节 [结果]:失败:responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength) [时间线]:时间线:{“请求开始时间”:504963459.698,“初始响应时间”:504963459.821,“请求完成时间”:504963459.821,“序列化完成时间”:504963459.821,“延迟”:0.123秒,“请求持续时间”:0.123秒,“序列化持续时间”:0.001秒,“总持续时间”:0.124秒} 可选(myurl.php?method=GET\u RECENT) 可选({URL:myurl.php?method=GET_RECENT}{状态代码:200,标题{ “缓存控制”=“最大年龄=0”; 连接=“保持活动”; “内容长度”=0; “内容类型”=“文本/html;字符集=UTF-8”; 日期=“2017年1月1日星期日11:41:31 GMT”; Expires=“2017年1月1日星期日11:41:31 GMT”; “保持活动”=“超时=7,最大=100”; Server=“Apache/2.2.15(CentOS)”; “X-Powered-By”=“PHP/5.6.25”; } }) 可选(0字节) 失败


我认为你设置的邮递员设置无效。要将
JSON
作为
GET
请求中的数据发送,您应该使用
raw
选项,如下图所示


我认为您正在设置的邮递员设置无效。要将
JSON
作为
GET
请求中的数据发送,您应该使用
raw
选项,如下图所示


您确定您的请求正文有效吗?对我来说,这似乎不是一个有效的
JSON
。我更喜欢。谢谢。我不知道JSON请求和html有什么不同。那么你能帮我吗?你能展示一下生成JSON输出的代码吗?以及您试图发布的完整JSON?另外,您在哪里收到此错误?这是Postman视图。我想下载这些数据。您确定您的请求正文有效吗?对我来说,这似乎不是一个有效的
JSON
。我更喜欢。谢谢。我不知道JSON请求和html有什么不同。那么你能帮我吗?你能展示一下生成JSON输出的代码吗?以及您试图发布的完整JSON?另外,您在哪里收到此错误?这是Postman视图。我想下载这些数据。非常感谢。我用另一个密码解决了它!你的回答很有帮助!非常感谢。我用另一个密码解决了它!你的回答很有帮助!