我請求通過JSON的一些數據,我是新來的SWIFT爲iOS,所以我沒有得到什麼在我的代碼的問題:錯誤而在SWIFT請求JSON + IOS
var URL="http://X.X.X.X.X/Api/UserManagement/getMobileUser";
var UserId=uname.text;
var pword=passwd.text;
var PostData: NSString = "{\"data\":{},\"action\":\"System\",\"method\":\"getMobileUser\",\"username\":\" mpc01\",\"password\":\"mpc01\",\"type\":\"rpc\",\"tid\":\"144\"}"
let request = NSMutableURLRequest(URL: NSURL(string: URL)!)
request.HTTPMethod = "POST"
let postString = PostData
//var st:NSData = PostData.dataUsingEncoding(NSUTF8StringEncoding)!
// request.HTTPBody = PostData.dataUsingEncoding(NSUTF8StringEncoding)
request.HTTPBody = (postString as NSString).dataUsingEncoding(NSUTF8StringEncoding)
//let data = (postString as NSString).dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil {
println("error=\(error)")
return
}
println("response = \(response)")
let responseString = NSString(data: data, encoding: NSUTF8StringEncoding)
println("responseString = \(responseString)")
}
task.resume()
響應我得到是:
response = {URL:http://X.X.X.X/Api/UserManagement/getMobileUser} {status code:500,headers'Cache-Control'=「no-cache」; 「Content-Length」= 36; 「Content-Type」=「application/json; charset = utf-8」; Date =「Sat,03 Jan 2015 06:11:51 GMT」; Expires =「-1」; Pragma =「no-cache」; Server =「Microsoft-IIS/7.5」; 「X-AspNet-Version」=「4.0.30319」; 「X-Powered-By」=「ASP.NET」; ) }} responseString =可選({「Message」:「發生了錯誤。」})
我假設你已經測試過服務API(使用其他客戶端)並且它可以工作。然後,您創建的HTTP請求不正確。我建議您嘗試將至少另外兩個HTTP頭文件設置爲'application/json'和'Content-Length'的Content-Type到UTF-8編碼數據的長度。 – Codo
好吧我要試一試 –