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....
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。