错误域= NSCocoaErrorDomain代码= 3840“字符0周围的无效值 [英] Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0

查看:899
本文介绍了错误域= NSCocoaErrorDomain代码= 3840“字符0周围的无效值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手,遇到了问题,我用post方法向服务器发出了请求,并且得到了不错的Json的响应,之后我又用get方法提出了一个请求,但出现此错误。

Hi I am new at swift and I got problem, I made a request to server with post method and I get response with good Json, after that I am makeing another request with get method but I get this error.

错误:
responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain = NSCocoaErrorDomain Code = 3840字符0附近的值无效。 UserInfo = {NSDebugDescription =字符0周围的值无效。})

Error: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.})

请求参数:

static func getInformationFromConfig(token: String, config: String, section : String, option: String) -> [String:Any] {

        let getInformationFromConfigparam: [String : Any] = ["jsonrpc": "2.0",
                                      "id": 1,
                                      "method": "call",
                                      "params": [ token, "uci", "get", [ "config": config, "section": section, "option": option]]
    ]

    return getInformationFromConfigparam
}



 public func device(token: String, loginCompletion: @escaping (Any) -> ()) {
    let deviceinfo = JsonRequests.getInformationFromConfig(token: token, config: "wireless", section: "@wifi-iface[0]", option: "mode")
    makeWebServiceCall(urlAddress: URL, requestMethod: .get, params: deviceinfo, completion: { (JSON : Any) in
        loginCompletion(JSON)
    })
}

请求:

private func makeWebServiceCall (urlAddress: String, requestMethod: HTTPMethod, params:[String:Any], completion: @escaping (_ JSON : Any) -> ()) {


    Alamofire.request(urlAddress, method: requestMethod, parameters: params, encoding: JSONEncoding.default).responseJSON{ response in


        switch response.result {
        case .success(let value):

            let json = JSON(value)

            if let jsonData = response.result.value {

                completion(jsonData)
            }


        case .failure(let error):

                completion("Failure Response: \(error)")

ResponseString响应:

 [Request]: GET http://192.168.1.1/ubus
[Response]: <NSHTTPURLResponse: 0x60000003c4a0> { URL: http://192.168.1.1/ubus } { status code: 400, headers {
Connection = "Keep-Alive";
"Content-Type" = "text/html";
"Keep-Alive" = "timeout=20";
"Transfer-Encoding" = Identity;
} }
[Data]: 35 bytes
    [Result]: FAILURE:     responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}))


推荐答案

该错误表明服务器的响应不是有效的JSON字符串。您可以尝试使用responseString代替responseJSON吗?

The Error saying that the response from server is not a valid JSON string. Can you try responseString instead of responseJSON like

Alamofire.request(urlAddress, method: requestMethod, parameters: params).responseString{ response in
    debugPrint(response)
}

请参见Xcode调试器的输出&根据您的需要进行更改。

See the Xcode debugger output & change it according to your need.

这篇关于错误域= NSCocoaErrorDomain代码= 3840“字符0周围的无效值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆