今天上面给了个url:https://apit3.51mch.com/api/PatInfo/GetPatInfo?jsonStr={"CardNo":"135403000800151","HashKey":"63d0ab17-8877-44ba-b17d-e6ad14032516"}叫我发post请求。我一脸蒙蔽。
反复研究后原来,key是jsonStr value是后面的{"CardNo":"135403000800151","HashKey":"63d0ab17-8877-44ba-b17d-e6ad14032516"}
具体发送代码:
let param = ["jsonStr":"{\"CardNo\":\"135403000800151\",\"HashKey\":\"63d0ab17-8877-44ba-b17d-e6ad14032516\"}"]
Alamofire.request(.POST, "https://apit3.51mch.com/api/PatInfo/GetPatInfo", parameters: param, encoding: .URLEncodedInURL, headers: nil).responseJSON { (resquest, response, result) in 后面省略}
还是小朋友帮我想出来- -,哎 做个记录。 这里重点提下encoding,这个编码格式是URLEncodedInURL.这个就是我要写这篇东西的关键。。我去查了相关资料,我觉得这里还是用英文复制粘粘上来比较准确。
Used to specify the way in which a set of parameters are applied to a URL request.
URL: Creates a query string to be set as or appended to any existing URL query forGET,HEAD, andDELETErequests, or set as the body for requests with any other HTTP method. TheContent-TypeHTTP header field of an encoded request with HTTP body is set toapplication/x-www-form-urlencoded; charset=utf-8. Since there is no published specification for how to encode collection types, the convention of appending[]to the key for array values (foo[]=1&foo[]=2), and appending the key surrounded by square brackets for nested dictionary values (foo[bar]=baz).
URLEncodedInURL: Creates query string to be set as or appended to any existing URL query. Uses the same implementation as the.URLcase, but always applies the encoded result to the URL.
JSON: UsesNSJSONSerializationto create a JSON representation of the parameters object, which is set as the body of the request. TheContent-TypeHTTP header field of an encoded request is set toapplication/json.
PropertyList: UsesNSPropertyListSerializationto create a plist representation of the parameters object, according to the associated format and write options values, which is set as the body of the request. TheContent-TypeHTTP header field of an encoded request is set toapplication/x-plist.
Custom: Uses the associated closure value to construct a new request given an existing request and parameters.
简单来说URLEncodedInURL就是创建要被设置为或附加到任何现有的网址查询的查询字符串。