2017-02-07 207 views
0

你好我在SWIFT新的和我的問題,我做了一個請求,服務器用post方法,我也得到響應與良好的JSON,之後,我makeing另一用GET方法請求,但我得到這個錯誤錯誤域= NSCocoaErrorDomain代碼= 3840「無效的周圍字符值0

錯誤: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(錯誤域= NSCocoaErrorDomain代碼= 3840 「無效圍繞字符值0」 的UserInfo = {NSDebugDescription =無效圍繞字符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.})) 
+0

(HTTP [周圍字符0 Alamofire無效值]的可能重複:// stackoverflow.com/questions/32355850/alamofire-invalid-value-around-character-0) –

回答

3

的錯誤說,從服務器的響應不是有效的JSON字符串。你可以嘗試,而不是responseString responseJSON像

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

見Xcode調試輸出&根據您的需要改變它。

+0

@EgleMatutyte,響應URL爲空。這意味着使用.get方法發送的參數有問題。 –

+0

我發現它是不好的令牌,但現在我修復它,並且我得到狀態代碼400錯誤,再次更新響應 –

-2

圖像的大小太大。超過了134,217,728字節的允許內存大小。您試圖在功能分配48771073個字節:

imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) 

相反的:

image = info[UIImagePickerControllerOriginalImage] 

用途:

image = info[UIImagePickerControllerEditedImage] 
+0

您可以添加一些格式到此答案嗎?此外,分成幾句話將使這個答案更清晰。 –

相關問題