Api返回text / html,它应该返回json

时间:2015-12-29 11:25:25

标签: php ios json api rest

我是网络服务开发的新手。我没有使用任何框架。我是从零开始做的。我想创建一个RESTFUL api服务,我想将它用于我的iOS应用程序。但是我收到了一些与内容类型相关的错误

这是我的api.php脚本 -

<?php

    require_once "scripts/Database.php";
    require_once "scripts/Database_Handler.php";
    require_once "scripts/config.php";

    header('Content-type:application/json;charset=utf-8'); //set the content type

    $db_obj = new Database(); 
    $db = $db_obj->get_database_connection(); //returns the database connection object
    $handler = new Database_Handler($db); //this is the class that does all the queries to database

    $method = $_SERVER['REQUEST_METHOD']; //method is POST here
    $url =  explode("/", $_SERVER['PATH_INFO']);
    $request = end($url);

    if($request == 'all_products'){
        $result_array = $handler->get_all_products();
    }
    else if ($request == 'customers'){
        $result_array = $handler->get_customers();
    }
    else{
        $result_array = $handler->get_all_products_available_in_stores();
    }

    echo json_encode($result_array);

?>

当我尝试在iOS中使用AFNetworking消耗资源时,它会出现以下错误。

"Request failed: unacceptable content-type: text/html" UserInfo={AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x7fe6b241c0b0> { URL: http://........../....../api.php/customers } { status code: 200, headers {
    Connection = "keep-alive";
    "Content-Encoding" = gzip;
    "Content-Type" = "text/html";
    Date = "Tue, 29 Dec 2015 11:16:01 GMT";
    Server = "nginx/1.8.0";
    "Transfer-Encoding" = Identity;
} }, NSLocalizedDescription=Request failed: unacceptable content-type: text/html, NSErrorFailingURLKey=http://........../....../api.php/customers}

任何人都可以帮助我从api端解决问题。

0 个答案:

没有答案