We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

setContentType('application/json', 'utf-8') issues~~

$app->get('/checkAccount', function () use ($app) {

    $app->response->setContentType('application/json', 'utf-8');

    $username = null;
    $password = null;

    if (isset($_GET['Username'])) {
        $username = $_GET['Username'];
    }
    if (isset($_GET['Password'])) {
        $password = $_GET['Password'];
    }

    $user_id = User::verifyUser($username, $password);
    if ($user_id == null) {
        $status = 'Username and password have not authorized';
    }
    else {
        $status = 'Username and password have authorized';
    }

    $data = array(
        'Username' => urlencode($username),
        'Password' => urlencode($password),
        'Status' => urlencode($status),
    );

    echo urldecode(json_encode($data));
});

I have checked response header, why always text/html? I have set setContentType('application/json', 'utf-8')!!!!!!!!!!!!!

Here is response: HTTP/1.1 200 OK Date: Sat, 07 Jun 2014 00:48:33 GMT Server: Apache/2.4.4 (Win64) PHP/5.4.12 X-Powered-By: PHP/5.4.12 Content-Length: 102 Keep-Alive: timeout=5, max=100 Connection: Keep-Alive Content-Type: text/html; charset=UTF-8

Page display is: {"Username":"sally","Password":"123456781","Status":"Username and password have authorized"}



18.6k

anyone can help me?



5.2k
Accepted
answer

Hi,

Why don't you use $this->response->setJsonContent()?

$app->get('/checkAccount', function () use ($app) {
        $response = new \Phalcon\Http\Response();

        // your logic here

        $response->setJsonContent($data);
        return $response;
});


18.6k

I have tried but same Content-Type: text/html; charset=UTF-8

This: $app->response->setContentType('application/json', 'utf-8');

should already be json but I do not know why still text/html, you can try too, i think this is bug!!!



8.1k

Just check your server setup on mod_mime and mime-types.



18.6k
edited Jun '14

???????? could you explain in details, i am a beginner, thx I have loaded mod_mime in Apache 2.4 with WAMP



8.1k
edited Jun '14

This depends of your system. In general case, see Apache manual.

https://httpd.apache.org/docs/current/mod/mod_mime.html#addtype

and /etc/mime.types



18.6k

How about Nginx, I am using test for Apache, but the production is Nginx 1.3.x



8.1k

https://nginx.org/en/docs/

I. For your attention

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

and check /etc/nginx/mime.types

This file doesn't have json settings by default.

II. Example for FPM describe in server section


 location ~* \.php$ {
   default_type  application/octet-stream;
   try_files $uri =404;
   fastcgi_pass 127.0.0.1:9000;
   include /etc/nginx/fastcgi_params;
   fastcgi_split_path_info  ^(.+\.php)(/.+)$;
   fastcgi_index index.php;
   fastcgi_param  SCRIPT_FILENAME /full/path/to/index.php;
   fastcgi_param  SCRIPT_NAME     /index.php;
   fastcgi_param  QUERY_STRING    $uri$args;
   fastcgi_param  REQUEST_METHOD  $request_method;
   fastcgi_param  CONTENT_TYPE    $content_type;
   fastcgi_param  CONTENT_LENGTH  $content_length;
   fastcgi_param  REQUEST_BODY    $request_body;
 }

III. Update your Nginx for safe - actual version is 1.6



18.6k

thanks, and could you help me to solve another issues, which is using message queue in phalcon? https://forum.phalcon.io/discussion/2482/phalcon-and-beanstalkd-how-to-use-it-