Reputation: 4330
When i send an array, im retreiving a wrong JSON in the server:
Client Object:
class Dummy : Mappable {
var test1 = "test1";
var test2 = "test2";
func mapping(map: Map) {
test1 <= map["test1"]
test2 <= map["test2"]
}
required init(){}
}
Client Calling:
let wrongDummies : [Dummy] = [Dummy(), Dummy()];
let wrongDummiesJSONArray = Mapper().toJSONArray(wrongDummies)
let dummies : [String:AnyObject] = [
"right": Mapper().toJSON(Dummy()),
"again_right": ["dummy1" : Mapper().toJSON(Dummy()), "dummy2" : Mapper().toJSON(Dummy())],
"wrong": [Mapper().toJSON(Dummy()), Mapper().toJSON(Dummy())],
"again_wrong": wrongDummiesJSONArray
]
println(dummies)
request(.POST, PROFILE_URL, parameters: dummies)
Client Print (Which seems OK):
[right: {
test1 = test1;
test2 = test2;
}, wrong: (
{
test1 = test1;
test2 = test2;
},
{
test1 = test1;
test2 = test2;
}
), again_right: {
dummy1 = {
test1 = test1;
test2 = test2;
};
dummy2 = {
test1 = test1;
test2 = test2;
};
}, again_wrong: (
{
test1 = test1;
test2 = test2;
},
{
test1 = test1;
test2 = test2;
}
)]
Server implementation (PHP):
ini_set("log_errors", 1);
ini_set("error_log", "$root/php-error.log");
error_log(print_r($_POST, true));
Response server:
Array
(
[again_right] => Array
(
[dummy2] => Array
(
[test2] => test2
[test1] => test1
)
[dummy1] => Array
(
[test2] => test2
[test1] => test1
)
)
[again_wrong] => Array
(
[0] => Array
(
[test2] => test2
)
[1] => Array
(
[test1] => test1
)
[2] => Array
(
[test2] => test2
)
[3] => Array
(
[test1] => test1
)
)
[right] => Array
(
[test2] => test2
[test1] => test1
)
[wrong] => Array
(
[0] => Array
(
[test2] => test2
)
[1] => Array
(
[test1] => test1
)
[2] => Array
(
[test2] => test2
)
[3] => Array
(
[test1] => test1
)
)
)
As you can see the objects inside the array are split by the number of their atributtes, which does not happen with the objects inside the dictionary.
Upvotes: 0
Views: 490
Reputation: 2013
In your logs, we see the data from the iOS side and the PHP side, but we can't really tell what is actually being transferred. We have to see what side the problem is actually happening, so see if you can print out the actual HTTP request that is being sent. If the JSON is wrong, then the iOS side is wrong, otherwise the PHP is reading it incorrectly.
First, make sure you are sending your parameters as JSON to the server. Alamofire will default to URL encoding by default, unless you specify that you want JSON.
Next, it looks like you're printing out the $_POST
which indicates that you're probably sending the data as URL encoded parameters, rather than JSON. That could be screwing it up.
If Alamofire is sending the data as JSON, and you get no data on the server, then make sure you change it to read the HTTP body with the JSON data in it:
$inputJSON = file_get_contents('php://input');
$input= json_decode( $inputJSON, TRUE );
I think that should probably cover your bases, or at least give you a good start as to where to look :)
Upvotes: 2