mercredi 1 août 2018

how to send parameter data to server?

Error Msg:

error("The data couldn’t be read because it isn’t in the correct format.")

func get3(url:String, searchString:String, sort_value:String, completion: @escaping (Result) -> Void) {
    var urlRequest = URLRequest(url: URL(string: url)!)

    urlRequest.httpMethod = "POST"

    let postString = "sort_value=\(sort_value)"+searchString

    urlRequest.httpBody = postString.data(using: String.Encoding.utf8)

    URLSession.shared.dataTask(with: urlRequest) { (data, responce, error) in

        if (error != nil)
        {
            let er = Result.error((error?.localizedDescription)!)
            print(er)
        }
        do{
            if let jsonResponce = try JSONSerialization.jsonObject(with:data!, options: .allowFragments) as? [String:Any]
            {
                completion(Result.success(jsonResponce))
            }
        } catch
        {
            let er = Result.error((error.localizedDescription))
            print(er)
        }
    }.resume()
}

@IBAction func clickOnSearchButtonForProductDisplay(_ sender: Any) {

    guard let searchName = searchTextField.text, searchName != "" else{
        return
    }

    let searchString = "search="+searchName+"&category_id="+categotyid

    let sort_value = "sort=sort_order&order=ASC"

    APLManager.shared.get3(url: "https://www.ravindrra.com/index.php?route=api/webservices/searchproduct", searchString: searchString, sort_value: sort_value, completion: { (result) in

        switch result
        {
        case .error(let msg):
            print("Error \(msg)")
        case .success(let json):
            print(json)
        }
    })
}




Aucun commentaire:

Enregistrer un commentaire