0

我正在尝试使用 ASIFormDataRequest for iphone 从我的 mysql 数据库中获取一些值。

从iphone我这样做:

NSURL *url = [NSURL URLWithString: ServerApiURLGet]; 
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL: url]; 
[request setDelegate:self]; 
[request setPostValue:@"james" forKey:@"name"];

请求被发送到一个 php 文件,它执行以下操作:

$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));

以及响应函数:

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;
}

回到 iphone,我尝试从回调中读取信息,如下所示:

- (void)requestFinished:(ASIHTTPRequest *)request
{        
NSDictionary *responseDict = [[request responseString] JSONValue];

NSLog(@"responseString: %@", [request responseString]);
NSLog(@"responseHeaders: %@", [request responseHeaders]);
NSLog(@"responseDict: %@", responseDict );
}

但它不起作用。我得到错误:

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)

非常感谢任何帮助。谢谢

编辑:

如果我传入一个简单的数组,结果相同:

    $newArray = array(
    "score1" => '1000', 
);

 sendResponse(200, json_encode($newArray)); 

解决了:

当我将 _GET 更改为 _POST 时,它可以工作。谢谢

4

1 回答 1

2

您只是将结果发送到 iPhone 而不对其进行 JSON 编码,因此 iPhone 端的 JSONValue 无法理解结果。

尝试类似的东西;

sendResponse(200, json_encode($result));

在发送之前对结果进行编码。

于 2012-03-12T07:28:18.430 回答