Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS不接受来自php web服务的json响应_Php_Objective C_Web Services_Afnetworking - Fatal编程技术网

iOS不接受来自php web服务的json响应

iOS不接受来自php web服务的json响应,php,objective-c,web-services,afnetworking,Php,Objective C,Web Services,Afnetworking,我是iOS的初学者。我有一个简单的web服务,它从表中检索数据并以JSON格式发送结果。我正在尝试从iOS与该web服务通信以接收JSON响应,但面临一些问题。这是我收到的错误: Request Failed with Error: Error Domain=AFNetworkingErrorDomain Code=-1016 "Expected content type {( "text/json", "application/json", "text/javascript" )},

我是iOS的初学者。我有一个简单的web服务,它从表中检索数据并以JSON格式发送结果。我正在尝试从iOS与该web服务通信以接收JSON响应,但面临一些问题。这是我收到的错误:

Request Failed with Error: Error Domain=AFNetworkingErrorDomain Code=-1016 "Expected     content type {(
"text/json",
"application/json",
"text/javascript"
)}, got text/html" UserInfo=0x7598e70
以下是我的代码片段:

PHP Web服务:

$stmt = "SELECT STORE_TYPE, STORE_NAME FROM STORE WHERE STORE_ZIP = $zip";
$result = mysqli_query($this->databaseconnection, $stmt);  
$storelist = array();
$store = array();
$jsondata;
while ($row = mysqli_fetch_assoc($result)) {
  $store['STORE_TYPE'] = $row['STORE_TYPE'];
  $store['STORE_NAME'] = $row['STORE_NAME'];
  array_push($storelist,$store);
}
$jsondata = json_encode($storelist);
echo $jsondata;
NSURL *url = [NSURL URLWithString:@"http://localhost/~Sandeep/store/store.php?rquest=getstores&zip=53715"];

NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation  JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    NSLog(@"%@", JSON);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
    NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
}];
[operation start];
在浏览器中执行php表单时,我得到以下结果:

[{"STORE_TYPE":"GROCERY","STORE_NAME":"Walmart"},{"STORE_TYPE":"BAKERY","STORE_NAME":"Lanes Bakery"},{"STORE_TYPE":"GROCERY","STORE_NAME":"Copps"}]
要与Web服务通信的iOS代码段:

$stmt = "SELECT STORE_TYPE, STORE_NAME FROM STORE WHERE STORE_ZIP = $zip";
$result = mysqli_query($this->databaseconnection, $stmt);  
$storelist = array();
$store = array();
$jsondata;
while ($row = mysqli_fetch_assoc($result)) {
  $store['STORE_TYPE'] = $row['STORE_TYPE'];
  $store['STORE_NAME'] = $row['STORE_NAME'];
  array_push($storelist,$store);
}
$jsondata = json_encode($storelist);
echo $jsondata;
NSURL *url = [NSURL URLWithString:@"http://localhost/~Sandeep/store/store.php?rquest=getstores&zip=53715"];

NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation  JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    NSLog(@"%@", JSON);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
    NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
}];
[operation start];
我看了很多教程,他们都说在php中对数组执行“json_encode”会以json格式对数据进行编码,而“echo”就是将编码后的json作为响应发送的方式。出于某种原因,我的iOS没有将其视为JSON。我不确定我在这里错过了什么/做错了什么

我非常感谢你在这方面的投入


谢谢

您需要设置正确的内容类型(使用),错误会列出可接受的类型,尽管您应该使用
application/json

$stmt = "SELECT STORE_TYPE, STORE_NAME FROM STORE WHERE STORE_ZIP = $zip";
$result = mysqli_query($this->databaseconnection, $stmt);  
$storelist = array();
$store = array();
$jsondata;
while ($row = mysqli_fetch_assoc($result)) {
  $store['STORE_TYPE'] = $row['STORE_TYPE'];
  $store['STORE_NAME'] = $row['STORE_NAME'];
  array_push($storelist,$store);
}
$jsondata = json_encode($storelist);
header('Content-Type: application/json');
echo $jsondata;

您需要设置正确的内容类型(使用),错误会列出可接受的类型,尽管您应该使用
application/json

$stmt = "SELECT STORE_TYPE, STORE_NAME FROM STORE WHERE STORE_ZIP = $zip";
$result = mysqli_query($this->databaseconnection, $stmt);  
$storelist = array();
$store = array();
$jsondata;
while ($row = mysqli_fetch_assoc($result)) {
  $store['STORE_TYPE'] = $row['STORE_TYPE'];
  $store['STORE_NAME'] = $row['STORE_NAME'];
  array_push($storelist,$store);
}
$jsondata = json_encode($storelist);
header('Content-Type: application/json');
echo $jsondata;

看起来你在回复中发送了HTML,而不是JSONLooks。看起来你在回复中发送了HTML,而不是JSONWorked。谢谢工作得很有魅力。谢谢