English 中文(简体)
使用 ASIFormData 请求发送 json 到php
原标题:Send json to php using ASIFormDataRequest

我在 iPphone 中是新来的, 我正试图将 NSMubable 数组转换为json 字符串, 然后使用请求发送此字符串到 php 文件, 然后用对 NSLog 的响应再次打印它, 以确保它被成功发送 。 所以我在查看中写入了以下代码DidLoad

NSString *phpUrl = @"http://dt-works.com/eman/bookMarks.php";

NSMutableArray *arrayKey = [[NSMutableArray alloc] initWithObjects:@"one", @"two", @"three", nil];
NSMutableArray *arrayValue1 = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", nil]; 
NSMutableArray *arrayValue2 = [[NSMutableArray alloc] initWithObjects:@"11", @"22", @"33", nil]; 

NSDictionary *theReqDictionary1 = [NSDictionary dictionaryWithObjects:arrayValue1 forKeys:arrayKey];
NSDictionary *theReqDictionary2 = [NSDictionary dictionaryWithObjects:arrayValue2 forKeys:arrayKey];

NSMutableArray *myArray = [NSMutableArray arrayWithObjects:theReqDictionary1,theReqDictionary2, nil];

NSString *jsonString = [myArray JSONRepresentation];

NSLog(@"%@", jsonString);



 NSDictionary *questions = nil;
NSURL *link = [NSURL URLWithString:[phpUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:link];
[request setPostValue:jsonString forKey:@"jsonString"];
[request setTimeOutSeconds:120];
[request setDelegate:self];
NSError *error = [request2 error];
[request2 startSynchronous];

if (!error) { 
    NSData *response = [request responseData];
    NSString *json = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
    questions = [json objectFromJSONString];
    NSLog(@"Data: %@", questions);

} 

书Marks.php的代码是:

$handle = fopen( php://input , r );
$jsonInput = $_POST[ jsonString ];
print_r($jsonInput);

但它给了我:

[{"one":"1","two":"2","three":"3"},{"one":"11","two":"22","three":"33"}]
Data: (null)

and I want Data to be as: [{"one":"1","two":"2","three":"3"},{"one":"11","two":"22","three":"33"}] how to do this??

最佳回答

尝试此选项, 而不是用密钥张贴 JSON 字符串, 您需要将它作为数据发布到脚本中才能读取 。 目前您的脚本中您甚至没有使用 $handle 来做任何事...

NSURL *link = [NSURL URLWithString:[phpUrl     stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:link];
[request appendPostData:[jsonString  dataUsingEncoding:NSUTF8StringEncoding]]; 
[request setTimeOutSeconds:120];
[request setDelegate:self];

And your php code..

$handle = fopen( php://input , r );
$jsonInput = fgets($handle);
$decoded = json_decode($jsonInput,true);
print_r($decoded);
问题回答

暂无回答




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签