使iPhone发送URLEncoded字符串而不是JSON [英] Make iPhone to send URLEncoded string instead of JSON
问题描述
我有一个Symfony2服务器从JQuery接收HTTP POSTs:
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");
这完美无缺,在内部转换一个Request对象的json,一旦在Controller中,使用 $ this-> getRequest() - > get('key1')
或 get('key2')
获得一些形式良好的PHP对象,无需额外的工作。
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.
所以我有一个完整的应用程序与JQuery和Symfony2这样工作。
So I have a complete application with JQuery and Symfony2 working this way.
现在我需要使用iPhone SDK和Objective C开发一个移动客户端。
Now I need to develop a mobile client using iPhone SDK and Objective C.
但是我发现要做的是发送JSON并在服务器中使用json_decode将其转换为PHP对象一次。这些例子使用这段代码:
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];
这会将JSON数据直接发送到服务器,并且 $ this-> ; getRequest() - > get('key1')
返回null 。此方法需要在HTTP请求的内容正文中接收该JSON,并使用PHP json_decode
来管理数据。
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.
好吧,我可以让它修改我的API来检查什么样的数据来了, json_decoding
如果是JSON或 getRequest
如果它是URLEncode。
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.
但在做这种解决方法之前,是不是有一个简单的方法来获得 JQuery
并行转换获得链接的 NSDictionaries
和 NSArrays
为 URLEncoded
并以 application / x-www-form-urlencoded
的形式发送到服务器,始终在 getRequest() - > get('key1')
style?
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?
推荐答案
这个工作完美,内部转换json在请求
对象,一旦在控制器,我可以直接用
$ this-> getRequest() - > get(' key1')或get('key2')获取一些井
形成没有额外工作的PHP对象。
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不在内部转换你的JSON在一个Request对象!
如果你想要一种处理数据的方法,你只需要发送一种类型的数据(json或x-www-url-encoded)。我在上一个问题写了你可以编码iOS发送数据与x-www-url编码,但你也可以在jQuery.post和iOS中发送JSON。然后只能实现:
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());
要在JS中发送JSON数据,您需要下一个代码:
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){},
});
这篇关于使iPhone发送URLEncoded字符串而不是JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!