在浏览器中获得json结果,但在iOS simualtor中没有


Getting json results in a browser but not in iOS simualtor

我通过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相关问题?

谢谢你的帮助。

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

<?php header("Content-type: application/json");
$host = "localhost"; //Your database host server
$db = "company"; //Your database name
//etc....
相关文章: