ios 字符 0 周围的 Alamofire 无效值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32355850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Alamofire invalid value around character 0
提问by Lord Vermillion
Alamofire.request(.GET, "url").authenticate(user: "", password: "").responseJSON() {
(request, response, json, error) in
println(error)
println(json)
}
This is my request with Alamofire, for a certain request it sometime works, but sometimes i get:
这是我对 Alamofire 的请求,对于某个请求,它有时会起作用,但有时我会得到:
Optional(Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn't be completed. (Cocoa error 3840.)" (Invalid value around character 0.) UserInfo=0x78e74b80 {NSDebugDescription=Invalid value around character 0.})
I've read that this can be due to invalid JSON, but the response is a static json string that i have validated in JSON validator as valid. It does contain ? ? ? characters and some HTML.
我读过这可能是由于无效的 JSON,但响应是一个静态的 json 字符串,我已经在 JSON 验证器中验证为有效。它包含 ? ? ? 字符和一些 HTML。
Why am i getting this error sometimes?
为什么我有时会收到此错误?
回答by Smit
I also faced same issue. I tried responseString
instead of responseJSON
and it worked. I guess this is a bug in Alamofire
with using it with django
.
我也面临同样的问题。我试过responseString
而不是,responseJSON
它奏效了。我想这是Alamofire
与django
.
回答by Avijit Nagare
I got same error while uploading image in multipart form in Alamofire as i was using
我在使用 Alamofire 时以多部分形式上传图像时遇到同样的错误
multipartFormData.appendBodyPart(data: image1Data, name: "file")
i fixed by replacing by
我通过替换固定
multipartFormData.appendBodyPart(data: image1Data, name: "file", fileName: "myImage.png", mimeType: "image/png")
Hope this help someone.
希望这有助于某人。
回答by Krutarth Patel
May this Help YOu
愿这对你有帮助
Alamofire.request(.GET, "YOUR_URL")
.validate()
.responseString { response in
print("Success: \(response.result.isSuccess)")
print("Response String: \(response.result.value)")
}
回答by cameronmoreau
The same issue happened to me and it actually ended up being a server issue since the content type wasn't set.
同样的问题发生在我身上,它实际上最终成为服务器问题,因为未设置内容类型。
Adding
添加
.validate(contentType: ["application/json"])
To the request chain solved it for me
到请求链为我解决了
Alamofire.request(.GET, "url")
.validate(contentType: ["application/json"])
.authenticate(user: "", password: "")
.responseJSON() { response in
switch response.result {
case .Success:
print("It worked!")
print(response.result.value)
case .Failure(let error):
print(error)
}
}
回答by Saeed
In my case , my server URL was incorrect. Check your server URL !!
就我而言,我的服务器 URL 不正确。检查您的服务器 URL !!
回答by Ram Madhavan
I got the same error. But i found the solution for it.
我得到了同样的错误。但我找到了解决方案。
NOTE 1: "It is not Alarmofire error", it's bcouse of server error.
注意 1:“这不是 Alarmofire 错误”,这是因为服务器错误。
NOTE 2: You don't need to change "responseJSON" to "responseString".
注意 2:您不需要将“responseJSON”更改为“responseString”。
public func fetchDataFromServerUsingXWWWFormUrlencoded(parameter:NSDictionary, completionHandler: @escaping (_ result:NSDictionary) -> Void) -> Void {
let headers = ["Content-Type": "application/x-www-form-urlencoded"]
let completeURL = "http://the_complete_url_here"
Alamofire.request(completeURL, method: .post, parameters: (parameter as! Parameters), encoding: URLEncoding.default, headers: headers).responseJSON { response in
if let JSON = response.result.value {
print("JSON: \(JSON)") // your JSONResponse result
completionHandler(JSON as! NSDictionary)
}
else {
print(response.result.error!)
}
}
}
回答by Ratz
This is how I managed to resolve the Invalid 3840 Err.
这就是我设法解决无效 3840 Err 的方法。
The error log
错误日志
responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}))
- It was with EncodingType used in the Request, The Encoding Type used should be acceptedin your Server-Side.
- 这是与编码类型请求中使用,编码类型使用应该是acceptedin你的服务器端。
In-order to know the Encoding I had to run through all the Encoding Types:
为了知道编码,我必须遍历所有编码类型:
default/ methodDependent/ queryString/ httpBody
默认/ methodDependent/ queryString/ httpBody
let headers: HTTPHeaders = [
"Authorization": "Info XXX",
"Accept": "application/json",
"Content-Type" :"application/json"
]
let parameters:Parameters = [
"items": [
"item1" : value,
"item2": value,
"item3" : value
]
]
Alamofire.request("URL",method: .post, parameters: parameters,encoding:URLEncoding.queryString, headers: headers).responseJSON { response in
debugPrint(response)
}
- It also depends upon the responsewe are recieving use the appropriate
- responseString
- responseJSON
- responseData
- 这也取决于我们收到的回应使用适当的
- 响应字符串
- 响应JSON
- 响应数据
If the response is not a JSON & just string in response use responseString
如果响应不是 JSON 而是响应中的字符串,请使用responseString
Example: in-case of login/ create token API :
示例:在登录/创建令牌 API 的情况下:
"20dsoqs0287349y4ka85u6f24gmr6pah"
responseString
“20dsoqs0287349y4ka85u6f24gmr6pah”
响应字符串
回答by Bruno Muniz
I solved using this as header:
我解决了使用它作为标题:
let header = ["Content-Type": "application/json",
"accept": "application/json"]
let header = ["Content-Type": "application/json",
"accept": "application/json"]
回答by agrippa
I was sending the improper type (String) to the server in my parameters (needed to be an Int).
我在我的参数中向服务器发送了不正确的类型(字符串)(需要是一个 Int)。
回答by krishnan
Error was resolved after adding encoding: JSONEncoding.default with Alamofire.
添加编码后错误已解决:JSONEncoding.default with Alamofire。
Alamofire.request(urlString, method: .post, parameters:
parameters,encoding:
JSONEncoding.default, headers: nil).responseJSON {
response in
switch response.result {
case .success:
print(response)
break
case .failure(let error):
print(error)
}
}