0

I am trying to pass the array of items as a param but I getting the error anyone please correct to me.

These are my params and url:

https://www.furnitureinfashion.net/FIF-APP/app_array_cart.php
let proids = ["100","200","300","400"]
let quantity = ["1","2","3","4"]
    let params:[String:Any] = ["product_id":proids,
                               "qty":quantity]

I getting the following error:

https://www.furnitureinfashion.net/FIF-APP/app_array_cart.php
["qty": ["1", "2", "3", "4"], "product_id": ["100", "200", "300", "400"]]
JSON could not be serialized because of error:
The data couldn’t be read because it isn’t in the correct format.
Error Optional(Alamofire.AFError.responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(error: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.})))
2019-11-26 15:59:53.612426+0530 Furniture in Fashion[13014:196121] Warning: Attempt to present <UIAlertController: 0x7f97d314e800>  on <Furniture_in_Fashion.WishListVC: 0x7f97d2ebaa20> which is already presenting (null)

please find my API manager class with the following.

 func apiDataarrayPostMethod(url:String,parameters:[String:Any] , completion: @escaping (_ data:[String:Any]? , _ error:Error?) -> Void)
    {
        UIApplication.shared.isNetworkActivityIndicatorVisible = true
        let manager = Alamofire.SessionManager.default
        manager.session.configuration.timeoutIntervalForRequest = 45

        manager.request(url, method:.post, parameters: parameters, encoding: JSONEncoding.default, headers: headersintoApi()).responseJSON { (response:DataResponse<Any>) in

            UIApplication.shared.isNetworkActivityIndicatorVisible = false 
            if response.result.isSuccess
            {
                print("Response Data: \(response)")

                if let data = response.result.value as? [String:Any]
                {
                    completion(data , nil)

                }else{
                    Helper.Alertmessage(title: "Alert", message: (response.error?.localizedDescription)!, vc: nil)

                    completion(nil,response.error)
                }
            }
            else
            {
                Helper.Alertmessage(title: "Alert", message: (response.error?.localizedDescription)!, vc: nil)
                completion(nil,response.error)
                print("Error \(String(describing: response.result.error))")
            }

        }
    }
  • If the error occurs at response time then the received data is not JSON. Create a string and print it. Most likely it's HTML. – vadian Nov 26 '19 at 11:19

2 Answers2

0

Your data isn't valid JSON. Using this website you can validate it. It should be: {"qty": ["1", "2", "3", "4"],"product_id": ["100", "200", "300", "400"]}.

It should start and end with { & }, instead of [ & ]. See also here.

Jeroen
  • 2,011
  • 2
  • 26
  • 50
0

Please try responseString instead of responseJSON. Explain in following answer.

https://stackoverflow.com/a/33541493/9673374

ketaki Damale
  • 634
  • 7
  • 25