0
我從後端開發人員處獲得了下面的post服務,現在我試圖在swift上實現它。錯誤:在Swift上發佈時
<form role="form" method="post" action=「order_post.asp" id="frmBack">
<input type="hidden" name="product_id" value="297">
<input type="hidden" name="order_no" value="010223005373">
<input type="hidden" name=「city」 value=「Paris」>
<input type="hidden" name=「county」 value=「La Fayette」>
<input type="hidden" name="price" value="143">
<input type="hidden" name=「receiver_name」 value=「Jane Doe」>
<input type="hidden" name=「sender_name」 value=「John Doe「>
<input type="submit" id="submit_back_handle">
我已經寫了下面的代碼,但它沒有工作,叫我填寫表格上的所有字段。有人可以告訴我我錯過了什麼嗎?
func post() {
let params = [
「product_id": "297",
「order_no": "010215135311",
「city」: 「Paris」,
「county」: 「La Fayette「,
"price": "143",
"receiver_name": 「Jane Doe「,
"sender_name": 「John Doe」,
] as Dictionary <String, String>
let request = NSMutableURLRequest(URL: NSURL(string: "http://webservis.mydomain.com/order_post.asp")!)
let session = NSURLSession.sharedSession()
request.HTTPMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(params, options: [])
let task = session.dataTaskWithRequest(request) { data, response, error in
guard data != nil else {
print("no data found: \(error)")
return
}
do {
if let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary {
let success = json["success"] as? Int // Okay, the `json` is here, let's get the value for 'success' out of it
print("Success: \(success)")
} else {
let cfEnc = CFStringEncodings.ISOLatin5
let enc = CFStringConvertEncodingToNSStringEncoding(CFStringEncoding(cfEnc.rawValue))
let jsonStr = NSString(data: data!, encoding: enc) // No error thrown, but not NSDictionary
print("Error could not parse JSON: \(jsonStr)")
}
} catch let parseError {
print(parseError) // Log the error thrown by `JSONObjectWithData`
let cfEnc = CFStringEncodings.ISOLatin5
let enc = CFStringConvertEncodingToNSStringEncoding(CFStringEncoding(cfEnc.rawValue))
let jsonStr = NSString(data: data!, encoding: enc)
print("Error could not parse JSON: '\(jsonStr)'")
}
}
task.resume()
}
當我嗅探網站時,迴應和請求標題如下;
響應頭
Cache-Control:private Content-Encoding:gzip Content-Length:6876 Content-Type:text/html Date:Sat, 02 Jan 2016 16:11:30 GMT Server:Microsoft-IIS/8.5 Set-Cookie:xxx; secure; path=/ Vary:Accept-Encoding X-Powered-By:ASP.NET X-Powered-By-Plesk:PleskWin
請求頭
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Accept-Encoding:gzip, deflate Accept-Language:en-US,en;q=0.8 Cache-Control:max-age=0 Connection:keep-alive Content-Length:526 Content-Type:application/x-www-form-urlencoded Cookie:xxx Host:xxx Origin:xxx Referer:xxx Upgrade-Insecure-Requests:1 User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) > AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36
什麼沒有準確地工作? – Qbyte
我得到以下錯誤; {「status」:「0」,「msg」:「請填寫空值!」,「name」:「Post Order」}。即使當我嘗試郵遞員時,我也有同樣的錯誤 – mehmeet43
你從哪裏得到錯誤? – Qbyte