Как решить нулевой ответ при получении данных с сервера с помощью JSON Swift и переполнения стека

Я пытаюсь отправлять и получать данные из приложения и сервера, используя Swift, JSON и PHP, но я получаю все нулевые значения.
ответ сервера:

 porte
<null>
venditore
<null>
cilindrata
<null>
ordine
<null>
catalizzatore
<null>
IVA
<null>
percentualeammortamento
<null>
cognome
<null>
garanzia
<null>
targa
<null>
alimentazione
<null>
marca
<null>
KM
<null>
sinistri
<null>
modello
<null>
telaio
<null>
indirizzo
<null>
data
<null>
colore
<null>
dossier
<null>

Вот мой код:

import UIKit
class SaveViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {var data_save:String! ="SomeString"var venditore_save:String! ="SomeString"var ordine_save:String! ="SomeString"var dossier_save:String! ="SomeString"var marca_save:String! ="SomeString"var modello_save:String! ="SomeString"var cilindrata_save:String! ="SomeString"var targa_save:String! ="SomeString"var telaio_save:String! ="SomeString"var alimentazione_save:String! ="SomeString"var catalizzatore_save:String! ="SomeString"var porte_save:String! ="SomeString"var colore_save:String! ="SomeString"var km_save:String! ="SomeString"var garanzia_save:String! ="SomeString"var incidenti_save:String! ="SomeString"var cognome_save:String! ="SomeString"var indirizzo_save:String! ="SomeString"var iva_save:Bool! ="SomeString"var percentualeammortamento_save:String! ="SomeString"
@IBAction func btnSignUp_Clicked(sender: UIButton) {
let parameters = ["data":data_save, "venditore":venditore_save, "dossier":dossier_save,"ordine":ordine_save, "cognome":cognome_save,"indirizzo":indirizzo_save, "marca":marca_save, "modello":modello_save, "targa":targa_save, "telaio":telaio_save, "cilindrata":cilindrata_save, "alimentazione":alimentazione_save, "catalizzatore":catalizzatore_save, "KM":km_save, "porte":porte_save, "colore":colore_save, "sinistri":incidenti_save, "garanzia":garanzia_save, "IVA": String(iva_save), "percentualeammortamento":percentualeammortamento_save] as Dictionary<String, String>let url = NSURL(string: "myurl") //change the urllet session = NSURLSession.sharedSession()let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "POST" //set http method as POST

let err: NSError?request.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(parameters, options:[])request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
let task = session.dataTaskWithRequest(request) { data, response, error in
guard data != nil else {
print("no data found: \(error)")
return
}do {
if let json = try NSJSONSerialization.JSONObjectWithData(data!, options:[]) as? NSDictionary {
let success = json["success"] as? Int

print("Success: \(success)")
} else {
let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)    // No error thrown, but not NSDictionary
print("Error could not parse JSON: \(jsonStr)")
}
} catch let parseError {
print(parseError)

let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("Error could not parse JSON: '\(jsonStr)'")
}
}

task.resume()
}
@IBAction  func btnLoadUser_Clicked(sender: UIButton) {

let url = NSURL(string: "myurl")

let request = NSMutableURLRequest(URL: url!)let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in

if data == nil {
print("request failed \(error)")
return
}do{

let json = try! NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers) as! NSDictionary

for (key, value) in json {let data = try! NSJSONSerialization.dataWithJSONObject([key, value], options:[])print(key)

print(value)}
}catch {
print("bom bom bom error")
}

}
task.resume() } }

Вот код php:

   <?php
$ricambio = array();
$ricambio['data'] = $_POST['data'];
$ricambio['venditore'] = $_POST['venditore'];
$ricambio['dossier'] = $_POST['dossier'];
$ricambio['ordine'] = $_POST['ordine'];
$ricambio['cognome'] = $_POST['cognome'];
$ricambio['indirizzo'] = $_POST['indirizzo'];
$ricambio['marca'] = $_POST['marca'];
$ricambio['modello'] = $_POST['modello'];
$ricambio['targa'] = $_POST['targa'];
$ricambio['telaio'] = $_POST['telaio'];
$ricambio['cilindrata'] = $_POST['cilindrata'];
$ricambio['alimentazione'] = $_POST['alimentazione'];
$ricambio['catalizzatore'] = $_POST['catalizzatore'];
$ricambio['KM'] = $_POST['KM'];
$ricambio['porte'] = $_POST['porte'];
$ricambio['colore'] = $_POST['colore'];
$ricambio['sinistri'] = $_POST['sinistri'];
$ricambio['garanzia'] = $_POST['garanzia'];
$ricambio['IVA'] = $_POST['IVA'];
$ricambio['percentualeammortamento'] = $_POST['percentualeammortamento'];

echo json_encode($ricambio);?>

Какие-либо решения? Может быть, проблема в вариантах: [] в

 try NSJSONSerialization.JSONObjectWithData(parameters, options:[]

0

Решение

При размещении тела json в PHP он не будет автоматически декодировать строку json в суперглобальный $ _POST.

Попробуйте что-то вроде

$jsonBody = file_get_contents('php://input');
$decoded = json_decode($jsonBody);

var_dump($decoded);
0

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]