I'm trying to use ASIFormDataRequest for iphone to get some values from my mysql database.
From the iphone I do this:
NSURL *url = [NSURL URLWithString: ServerApiURLGet];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL: url];
[request setDelegate:self];
[request setPostValue:@"james" forKey:@"name"];
The request is send to a php files, which does this:
$con = mysql_connect("host","user","password");
mysql_select_db("table", $con);
$name = mysql_real_escape_string($_GET['name']);
$query = mysql_query("SELECT score FROM `active_users` WHERE `nickname` ='$name';");
$result = mysql_fetch_array($query);
sendResponse(200, json_encode($result));
And the response function:
function sendResponse($status = 200, $body = '', $content_type = 'text/html')
{
$status_header = 'HTTP/1.1 ' . $status . ' ' . getStatusCodeMessage($status);
header($status_header);
header('Content-type: ' . $content_type);
echo $body;
}
Back to the iphone, I try to read the information from the callback like this:
- (void)requestFinished:(ASIHTTPRequest *)request
{
NSDictionary *responseDict = [[request responseString] JSONValue];
NSLog(@"responseString: %@", [request responseString]);
NSLog(@"responseHeaders: %@", [request responseHeaders]);
NSLog(@"responseDict: %@", responseDict );
}
But it doesn't work. I get the error:
2012-03-12 08:33:57.657 Test[1991:707] -JSONValue failed. Error is: Unexpected end of input
2012-03-12 08:33:57.659 Test[1991:707] responseString:
2012-03-12 08:33:57.660 Test[1991:707] responseHeaders: {
Connection = close;
"Content-Type" = "text/html";
Date = "Mon, 12 Mar 2012 07:33:58 GMT";
Server = "Apache/2.2.6 mod_auth_kerb/5.3 PHP/5.2.17 mod_fcgid/2.3.6";
"Transfer-Encoding" = Identity;
"X-Powered-By" = "PHP/5.2.17";
}
2012-03-12 08:33:57.661 Test[1991:707] responseDict: (null)
Any help is very appreciated. Thanks
Edit:
Same result if I pass in a simple array:
$newArray = array(
"score1" => '1000',
);
sendResponse(200, json_encode($newArray));
Solved:
When I changed _GET to _POST, it works. Thanks