没有在http主体的Alamofire请求中接收json数据

时间:2017-01-27 04:41:39

标签: json swift2 alamofire

我使用以下代码向带有http参数的Web服务器发送POST请求,以便接收JSON数据

Alamofire.request(.POST,myURL, parameters: [:], encoding: .Custom({
        (convertible, params) in
        var mutableRequest = convertible.URLRequest.copy() as NSMutableURLRequest
        mutableRequest.HTTPBody = "MyBody".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
        return (mutableRequest, nil)
    }))

我从stackoverflow得到它:POST request with a simple string in body with Alamofire 但我没有收到任何数据。 即时通讯使用swift 2.3和alamofire 3.5。

任何帮助?

1 个答案:

答案 0 :(得分:0)

简单 POST Alamofire4 & Swift3

    let url: String = "https://example.com"
    let parameter = ["X": "10", "Y": "20"]


    Alamofire.request(url, method: .post, parameters: parameter, encoding: URLEncoding.default, headers: nil)
        .responseJSON { (response) in

            let result = response.result.value
            print(result)

    }

简单 GET 调用以查看一些 JSON 结果:

        let url: String = "https://example.com"
        Alamofire.request(url)
        .responseJSON { response in

            let result = response.result.value
            print(result)
    }