php - The data couldn’t be read because it isn’t in the correct format error in Swift 3 -
i'm working on project have access external database in order insert data. i'm using swift 3 , below swift 3 code , php code.
func applybuttonpressedfunction(sender:uibarbuttonitem) { let urlofsmartcf = url(string: "http://192.168.1.99/insertdata.php") let request = nsmutableurlrequest(url: urlofsmartcf! url) request.httpmethod="post" request.addvalue("application/json", forhttpheaderfield: "accept") contact in contactscaptuure { let usermobilenumber = dbmanager.shared.retriveregisterednumberofmobile() print(string(describing: usermobilenumber!)) print(contact.phonenumber!) let postparameters = "{\"usermobilenum\":\(string(describing: usermobilenumber!)),\"contactnum\":\(contact.phonenumber!)}"; print(postparameters) request.httpbody = postparameters.data(using: string.encoding.utf8) let task = urlsession.shared.datatask(with: request urlrequest) { data, response, error in if error != nil { print("error \(string(describing: error))") return; } { let myjson = try jsonserialization.jsonobject(with: data!, options: .mutablecontainers) as? nsdictionary if let parsejson = myjson { var msg : string! msg = parsejson["message"] as! string? print(msg) } } catch { print(error.localizeddescription) print(error) } } print("done") task.resume() } }
insertdata.php php code:
<?php if($_server["request_method"]=="post") { require 'connectdb.php'; $userphone = $_post["usermobilenum"]; $contactnum = $_post["contactnum"]; // codes }
however gives me 2 errors when perform function. error[1] error.localization , other came catch.
[1]the data couldn’t read because isn’t in correct format error [2]error domain=nscocoaerrordomain code=3840 "no value." userinfo={nsdebugdescription=no value.}
what did
since error occurs when emtpy data passed jsonobjectwithdata , php server has sent response empty body far know. checked postparameters string value , evaluated via online page check whether in valid json format , string passed. still couldn't identify problem.
went through lots of web sites , questions hours , came here ask help. appriciate if can give hand here.
Comments
Post a Comment