gpt4 book ai didi

php - 在浏览器中获取 json 结果,但在 iOS 模拟器中不行

转载 作者:行者123 更新时间:2023-11-30 00:56:34 24 4
gpt4 key购买 nike

我通过该 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....

关于php - 在浏览器中获取 json 结果,但在 iOS 模拟器中不行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20501792/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com