douzhongqiu5032 2013-12-10 17:54
浏览 22
已采纳

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

I'm getting database data through that php file:

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

?>

All is working fine in the browser, I'm getting all data, however, I don't get anything in the iOS simulator, without any php error log.

-(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];

}

What could be the problem? iOS or php related problem ?

Thank you for your help.

  • 写回答

1条回答 默认 最新

  • dongyan5239 2013-12-10 18:01
    关注

    Problem solved! Forgot to add header("Content-type: application/json");

    <?php header("Content-type: application/json");
    
    $host = "localhost"; //Your database host server
    $db = "company"; //Your database name
    //etc....
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装