[英]Issues with posting to php page using SBJSON on iPhone
I am lending the code from a previous project I have done where it worked perfectly. 我将代码从我之前完成过的项目中完美地工作了。 I have tested the page separately which outputs the data using jQuery and it works fine so I know it is an issue within xcode.
我已经分别测试了使用jQuery输出数据的页面,并且效果很好,所以我知道这是xcode中的问题。
I am using the following code which takes an NSDictionary containing the post data used to get the correct return data: 我正在使用以下代码,该代码采用一个NSDictionary,其中包含用于获取正确返回数据的发布数据:
-(NSDictionary *)loadData:(NSDictionary *) sendDict{
SBJsonWriter *writer = [SBJsonWriter new];
NSString * jsonData = [writer stringWithObject:sendDict];
NSString * getString = [NSString stringWithFormat:@"json=%@", jsonData];
NSData *requestData = [NSData dataWithBytes: [getString UTF8String] length: [getString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://divisi.co.uk/YTA/custom_api.php"]];
[request setHTTPMethod: @"POST"];
[request setHTTPBody: requestData];
NSData *returnData = [ NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];
NSLog(@"Returned Json: %@", returnString);
// decoding the json
NSDictionary *loginArray = [NSDictionary alloc];
loginArray = [returnString JSONValue];
if([[loginArray objectForKey:@"SC"] isEqualToString:@"200"]){
return [loginArray objectForKey:@"data"];
}
else{
return [loginArray objectForKey:@"error_message"];
}
}
When I send the post request it finds null on the php side and the json this encodes to be posted is {"method":"getWhatsOn"}
, very simple. 当我发送发布请求时,它在php端发现null,并且此编码要发布的json是
{"method":"getWhatsOn"}
,非常简单。 My test php page just gets this method name and prints it back encoded correctly ie {"SC":"200","data":"getWhatsOn"}
. 我的测试php页面仅获取此方法名称并正确地将其打印回编码,即
{"SC":"200","data":"getWhatsOn"}
。 Which works in browser using jQuery as I mentioned, but in php the $_POST['method'] returns null when the request comes from the iPhone. 如前所述,它在使用jQuery的浏览器中有效,但是在php中,当请求来自iPhone时,$ _POST ['method']返回null。
NSString *Sc= @"200";
NSString *dataD=@"getWhatsOn";
NSMutableDictionary *dictionnary = [NSMutableDictionary dictionary];
[dictionnary setObject:Sc forKey:@"firstString"];
[dictionnary setObject:dataD forKey:@"secondfield"];
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionnary
options:kNilOptions
error:&error];
NSString *urlString = @"your URL";
NSURL *url = [NSURL URLWithString:urlString];
// Prepare the request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"json" forHTTPHeaderField:@"Data-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:jsonData];
NSError *errorReturned = nil;
NSURLResponse *theResponse =[[NSURLResponse alloc]init];
NSData *data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&theResponse
error:&errorReturned];
if (errorReturned)
{
//...handle the error
}
else
{
NSString *retVal = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSLog(@"%@", retVal);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.