【发布时间】:2016-01-02 17:08:55
【问题描述】:
我从后端开发人员那里得到了以下后期服务,现在我正在尝试在 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
【问题讨论】:
-
什么不完全有效?
-
我收到以下错误; { "status": "0", "msg": "请填写空值!", "name": "Post Order" }.即使我尝试使用 Postman,我也遇到了同样的错误
-
你从哪里得到错误?
-
我在 else 块上有错误。
-
我应该把下面的提交行放到请求或字典的任何地方吗?