php - ASIHTTPRequest - 将 JSON 发布到 PHP

标签 php ios json asihttprequest

我过去曾多次使用 ASIHTTPRequest 将数据发送到 php 脚本,没有任何问题。这是我第一次尝试发送 JSON,但无法让它工作。

NSError *error = nil;
NSMutableDictionary *jsonDict = [NSMutableDictionary dictionaryWithObjects:objectArray forKeys:keyArray];
[jsonDict setObject:email forKey:@"email"];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict options:NSJSONWritingPrettyPrinted error:&error];

NSURL *url = [NSURL URLWithString:@"http://192.168.1.12:8888/Folder/script.php"];
__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setRequestMethod:@"POST"];
[request appendPostData:jsonData];
[request addRequestHeader:@"Content-Type" value:@"application/json"];

[request setCompletionBlock:^{
    NSString *responseString = [request responseString];
    NSLog(@"finished text: %@", responseString);
}];

[request setFailedBlock:^{
    NSError *error = [request error];
    NSLog(@"failed: %@", error);
}];

[request startAsynchronous];

现在的 PHP 脚本只是

<?php print_r($_POST); ?>

结果是:Array ()。什么也没有发生。知道问题可能是什么吗?

最佳答案

这不是你的 Objective-C 代码的问题,而是你的 PHP 脚本的问题。参见 this answer .当发送数据不是 application/x-www-form-urlencoded 时,您必须从 php://input 读取数据。 .

关于php - ASIHTTPRequest - 将 JSON 发布到 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17244350/

相关文章:

php - AJAX 无法 POST 到 PHP 文件

ios - iOS 7 上的自动布局崩溃,在 iOS 8 上运行良好

ios - 用于 JSON 解析的 AFNetworking 出错

php - 使用mysql生成并插入五位随机数

php - 列出所有 PHP 变量

php - 登录脚本无法正确读取密码?

ios - 如何在Objective-C中使用发件人属性

iphone - 同时使用触摸和触摸手势识别器

ios - json 格式的顺序正在改变

php - JSON 在 Firefox/Opera 中输出 null,在 Chrome 中正确