获取json会导致浏览器,但不会导致iOS模拟器

时间:2013-12-10 17:54:53

标签: php mysql ios mysqli

我通过该php文件获取数据库数据:

<?php

$host = "localhost"; //Your database host server
$db = "company"; //Your database name
$user = "root"; //Your database user
$pass = "root"; //Your password
$connection = mysqli_connect($host, $user, $pass);
//Check to see if we can connect to the server
if(!$connection)
{
    die("Database server connection failed.");
    die(mysqli_error($db)); 
}
else
{
    //Attempt to select the database
    $dbconnect = mysqli_select_db($connection, $db);
    //Check to see if we could select the database
    if(!$dbconnect)
    {
        die("Unable to connect to the specified database!");
    }
    else
    {
        $tag=$_GET["tag"];
        $resultset = mysqli_query($connection,"SELECT job,name FROM Employees where name LIKE '% $tag %'");
        $records = array();
        //Loop through all our records and add them to our array
        while($r = mysqli_fetch_assoc($resultset))
        {
            $records[] = $r;    
        }
        //Output the data as JSON
        echo json_encode($records);
    }
}

?>

所有在浏览器中工作正常,我得到所有数据,但是,我没有在iOS模拟器中得到任何东西,没有任何php错误日志。

-(void)getnameByTag:(NSString*)tag{

    NSString *url = [@"http://localhost/get_name_by_tag.php?tag=" stringByAppendingString:tag];
    NSLog(@"%@",url);

    NSURL *urll= [ NSURL URLWithString:url];


    NSURLRequest *request = [NSURLRequest requestWithURL:urll];

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

        arrayofJobs=[JSON valueForKeyPath:@"job"];
        arrayofNames=[JSON valueForKeyPath:@"name"];


        NSLog(@"authors: %@",arrayofJobs);

        [[self employeesTableView] reloadData];



    } failure:nil];


    [operation start];

}

可能是什么问题? iOS或php相关问题?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

问题解决了! 忘记添加header("Content-type: application/json");

<?php header("Content-type: application/json");

$host = "localhost"; //Your database host server
$db = "company"; //Your database name
//etc....