[英]Alamofire post json and response json
i'm try to post a JSON using Swift3 and Alamofire and it work successfully in Postman Postman screen shot but in code the response is HTML string that means an exception in server i tried to change encoding from JsonEncoding.default
to URLEncoding.default
and it works good but after 3 days the same error when i run the app 我尝试使用Swift3和Alamofire发布JSON,并且可以在Postman Postman屏幕截图中成功运行,但是在代码中响应为HTML字符串,这意味着服务器中存在异常,我尝试将编码从JsonEncoding.default
更改为URLEncoding.default
,并且运作良好,但三天后我运行应用程序时出现相同的错误
let url = "http://mattam.net/mobileapp/addOrder"
let par:[String:Any] = ["order_restaurant":8,
"order_type":1,
"order_address":1,
"order_within":"45 mins",
"order_exacttime":"09:00 pm",
"order_total":300,
"order_fees":30,
"order_gtotal":330,
"order_user":38,
"pquantity[10]":3,
"pquantity[9]":1,
"poption[9]":238,
"pextra[10]":"80,81"]
print(par)
Alamofire.request(url, method: .post, parameters: par, encoding: URLEncoding.default).responseJSON{
r in
if r.result.isSuccess{print("------i______i-----")}
print(r)
if let result = r.result.value as? NSDictionary{
print(result)}
}
and in PostMan Bulk edit is 在PostMan中,批量编辑是
order_restaurant:8
order_type:1
order_address:1
order_within:45 mins
order_exacttime:09:00 pm
order_total:300
order_fees:30
order_gtotal:330
order_user:38
pquantity[10]:3
pquantity[9]:1
poption[9]:238
pextra[10]:80,81
and url is "let url = " http://mattam.net/mobileapp/addOrder "" 网址是“ let url =” http://mattam.net/mobileapp/addOrder “”
Your problem is that your using http
instead of https
in your app. 您的问题是您在应用程序中使用http
而不是https
。 The screenshot uses https
while the url you posted (copied from your code) uses http
. 屏幕截图使用https
而您发布的网址(从代码中复制)使用http
。
If I understand your question right, you need to send some post details to the server as a Json, so here is some code to do that: 如果我正确理解您的问题,则需要以Json的身份将一些帖子详细信息发送到服务器,因此这里有一些代码可以做到这一点:
private func alamoFireAjax(url: String, parameters: Parameters, callback: @escaping (DataResponse<Any>) -> Void) {
Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default).responseJSON(completionHandler: callback)
}
I had a similar issue and to solve it I placed the dictionary creation in a method call. 我有一个类似的问题,为了解决这个问题,我将字典创建放在了一个方法调用中。 You can usually get away with most requests but I found anything larger than 10 rows needed a separate method handler. 通常,您可以避免大多数请求,但是我发现大于10行的任何内容都需要单独的方法处理程序。
fileprivate func generateParams() -> [String: Any] {
var params = [String: Any]()
params["order_restaurant"] = 8
params["order_type"] = 1
params["order_address"] = 1
params["order_within"] = "45 mins"
params["order_exacttime"] = "09:00 pm"
params["order_total"] = 300
params["order_fees"] = 30
params["order_gtotal"] = 330
params["order_user"] = 38
params["pquantity[10]"] = 3
params["pquantity[9]"] = 1
params["poption[9]"] = 238
params["pextra[10]"] = "80,81"
return params
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.