0
當我嘗試將一些數據發佈到服務器時,我遇到服務錯誤。我相信這個錯誤與我的請求結構有關。我正在使用AFNetworking和我的請求看起來如下:AFNetworking POST錯誤3840
lazy var networkManager: AFHTTPRequestOperationManager = {
var networkManager : AFHTTPRequestOperationManager = AFHTTPRequestOperationManager()
networkManager.requestSerializer.setValue("application/json",
forHTTPHeaderField: "Content-Type")
networkManager.responseSerializer.acceptableContentTypes =
NSSet(array: ["text/plain", "text/html", "application/json"]) as Set<NSObject>
return networkManager
}()
func createNewAccount(username:String, password:String, onCompletion:Closures.ServiceResponse) -> Void{
let parameters: [String: AnyObject] = [
"newUser": [
"name": username,
"password": password
]
]
var err : NSError?
var jsonData = NSJSONSerialization.dataWithJSONObject(parameters, options: nil, error: &err)
var jsonString = NSString(data: jsonData!, encoding: NSUTF8StringEncoding)
networkManager.POST(Constants.STAGING_URL + "/Services/UserService/UserService.svc/NewUserSignup",
parameters: jsonString,
success: { (operation: AFHTTPRequestOperation!,responseObject: AnyObject!) in
println(responseObject)
},
failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in
println(error.localizedDescription)
println(operation.responseString)
})
}
我試過參數選項的變化,我仍遇到同樣的問題。
現在,這裏是我的手的作品就好了,並將數據發送到服務器寫成預期的方法:
func signupNewUser(userName : String, password : String){
let jsonObject: [String: AnyObject] = [
"newUser": [
"name": userName,
"password": password
]
]
var urlString = Constants.STAGING_URL + "/Services/UserService/UserService.svc/NewUserSignup"
var request = NSMutableURLRequest(URL: NSURL(string: urlString)!)
var session = NSURLSession.sharedSession()
request.HTTPMethod = "POST"
var err: NSError?
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(jsonObject, options: nil, error: &err)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
var jsonData = NSJSONSerialization.dataWithJSONObject(jsonObject, options: nil, error: &err)
var jsonString = NSString(data: jsonData!, encoding: NSUTF8StringEncoding)
request.setValue(jsonString! as String, forHTTPHeaderField: "json")
var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
println(response)
})
task.resume()
}
上述要求工作得很好,並且服務器可以處理傳入的JSON和保存它在數據庫中成功。
我的服務器是一個WCF服務。該方法的接口我打電話看起來像這樣:
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/NewUserSignup")]
Response AddNewUser(User newUser);
實際NSError是:
Printing description of error:
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x7f98a0c58910 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set., NSUnderlyingError=0x7f98a0c582f0 "Request failed: bad request (400)"}
我假定服務器是好的,它只是具有與正確配置的要求做AFNetworking。任何幫助正確配置請求將不勝感激。