gpt4 book ai didi

ios - AFNetworking POST 错误 3840

转载 作者:行者123 更新时间:2023-11-30 14:17:16 25 4
gpt4 key购买 nike

当我尝试将一些数据发布到服务器时遇到服务错误。我确信该错误与我的请求结构有关。我正在使用 AFNetworking,我的请求如下所示:

      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 服务。我调用的方法的接口(interface)如下所示:

  [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 正确配置请求有关。任何帮助正确配置请求的帮助将不胜感激。

最佳答案

我的案例的解决方案很简单,与 NetworkManager 的配置有关。

这对我有用,因为 NetWorkManager 将从服务器接收 JSON:

 lazy var networkManager: AFHTTPRequestOperationManager = {

var networkManager : AFHTTPRequestOperationManager = AFHTTPRequestOperationManager()

var jsonRequestSerializer = AFJSONRequestSerializer()

networkManager.requestSerializer = jsonRequestSerializer

return networkManager
}()

关于ios - AFNetworking POST 错误 3840,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30969880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com