I have a Symfony2 server that receives HTTP POSTs from JQuery using:
var myJSON = {key1: "value1", key2: ["value2", "value3"]};
$.post(myURL, {myJSON}, function(json){}, "json");
This works perfect and internally converts the json in a Request object that, once in the Controller, I can get straightforward with $this->getRequest()->get('key1')
or get('key2')
obtaining some well formed PHP objects with no additional work.
So I have a complete application with JQuery and Symfony2 working this way.
Now I need to develop a mobile client using iPhone SDK and Objective C.
But all examples I am finding to do it send the JSON and convert it to PHP objects once in the server using json_decode. Those examples use this piece of code:
NSArray *myArray = [NSArray arrayWithObjects: @"Value2", @"Value3", nil];
NSDictionary *myDict = [NSDictionary dictionaryWithObjectsAndKeys: @"Value1", @"key1", myArray, @"Value2"];
NSData* requestData = [NSJSONSerialization dataWithJSONObject:myDict options:NSJSONWritingPrettyPrinted error:&error];
NSLog(@"JSON string:%@", [[NSString alloc] initWithData:requestData encoding:NSUTF8StringEncoding]); // To check that the conversion to JSON is indeed being performed perfectly!!
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"XMLHttpRequest" forHTTPHeaderField:@"X-Requested-With"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];
This sends directly the JSON data to the server and the $this->getRequest()->get('key1')
returns null. This approach expects to receive that JSON thata in the content body of the HTTP Request and do a PHP json_decode
to manage the data.
Ok, I can get it modifying my "API" to check what kind of data is coming and json_decoding
if it is JSON or the getRequest
thing if it is URLEncode.
But before doing that workaround, isn't there an easy way to get a JQuery
parallel conversion getting the linked NSDictionaries
and NSArrays
to be URLEncoded
and sent to the server as application/x-www-form-urlencoded
to get the data in the server always in the getRequest()->get('key1')
style?
This works perfect and internally converts the json in a Request object that, once in the Controller, I can get straightforward with $this->getRequest()->get('key1') or get('key2') obtaining some well formed PHP objects with no additional work.
Symfony does not internally convert your json in a Request object! If you want one way for working with data you need to send data with only one type (json or x-www-url-encoded). I wrote you in the previous question that you can encode iOS to send data with x-www-url-encoded but you can also send JSON in jQuery.post and from iOS. And then you must implement only:
$data = json_decode($this->getRequest()->getContent());
To send JSON data in your JS you need next code:
$.ajax({
type: "POST",
url: myURL,
data: JSON.stringify(myJSON),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(json){},
});