我是iOS新手。我试图通过post方法将数据发送到PHP。在PHP中,它无法获取$_POST['data']
之类的数据,但它需要$_GET['data']
。我的iOS代码如下。
NSString *strURL = [NSString stringWithFormat:@"http://example.com/app_respond_to_job?emp_id=%@&job_code=%@&status=Worker-Accepted&comment=%@",SaveID2,txtJobcode1,alertTextField.text];
NSURL *apiURL = [NSURL URLWithString:strURL];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:apiURL];
[urlRequest setHTTPMethod:@"POST"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
_receivedData = [[NSMutableData alloc] init];
[connection start];
NSLog(@"URL---%@",strURL);
有人可以解释为什么会这样,这会非常有帮助。
答案 0 :(得分:3)
请下载此文件https://www.dropbox.com/s/tggf5rru7l3n53m/AFNetworking.zip?dl=0
并在项目中导入文件 定义在#import" AFHTTPRequestOperationManager.h"
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"Your Url"]];
NSDictionary *parameters = @{@"emp_id":SaveID2,@"job_code":txtJobcode1.text,@"status":alertTextField.text};
AFHTTPRequestOperation *op = [manager POST:@"rest.of.url" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[responseObject valueForKey: @"data"];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@ ***** %@", operation.responseString, error);
}];
[op start];
答案 1 :(得分:0)
因为您通过网址中的查询字符串发送数据
我认为如果您尝试在请求的正文中传递数据,它会起作用:[urlRequest setHTTPBody:...]
答案 2 :(得分:0)
POST参数来自请求正文,而不是来自URL字符串。您需要:
setHTTPBody
并提供URL编码的字符串(无问号,IIRC)作为正文数据setValue:forHTTPHeaderField:
,将内容类型设置为application/x-www-form-urlencoded
[connection start]
的通话或使用initWithRequest:delegate:startImmediately:
,这样您就不会再次开始连接。最后一个很重要。如果尝试两次连接,可能会得到奇怪的结果。 : - )