dimanche 25 septembre 2016

lengthComputable always returns false doing ajax request

Hi there I'm trying to do an ajax request and response that uses lengthComputable on the xhr request. At the moment it returns false but I've set the Content-Length for the request and response and the request is not setting the header of 'Content-Length' but it does for the response. How do I set the header for the request in the class AddContentLength which is below. I did some research and from the question and answers that I found they said that you needed the request and response to have 'Content-Length' header for both of them for lengthComputable to return true so I can get percentages on a request. What do I need to do to get lengthComputable to work.

public function getcontacts(Request $request) {

    $page = $request->get('page') ?: 1;
    $sortby = $request->get('sortby') ?: 'id';
    $direction = $request->get('direction') ?: 1;
    $search_term = $request->get('search_term') ?: null;
    $contacts = null;

    $parameters = [
        'page' => $page,
        'sortby' => $sortby,
        'direction' => $direction,
        'search_term' => $search_term,
        'baseUrl' => '/admin/contacts/getcontacts',

    ];

    if($direction == 1) {
        $direction = 'asc';
    } else {
        $direction = 'desc';
    }


    if($search_term != null) {
        $contacts = Contact::where('firstname', 'like', '%' . $search_term . '%')
                            ->orWhere('lastname', 'like', '%' . $search_term . '%')
                            ->orWhere('company',  'like', '%' . $search_term . '%')
                            ->orWhere('email_address',  'like', '%' . $search_term . '%')
                            ->with('user')
                            ->orderBy($sortby, $direction)
                            ->paginate(10)->toArray();
    } else {
        $contacts = Contact::with('user')->orderBy($sortby, $direction)->paginate(10)->toArray();   
    }

    $data = array_merge($parameters, $contacts);

    return response()->json($data, 200, ['Content-Length' => strlen(json_encode($data))]);
}

middleware for requests:

<?php namespace App\Http\Middleware;

use Closure;

class AddContentLength {

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        // to be sure nothing was already output (by an echo statement or something)
        if (headers_sent() || ob_get_contents() != '') return $response;

        $content = $response->content();
        $contentLength = strlen($content);
        $useCompressedOutput = ($contentLength && isset($_SERVER['HTTP_ACCEPT_ENCODING']) &&
           strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false);



        if ($useCompressedOutput) {

            // In order to accurately set Content-Length, we have to compress the data ourselves 
            // rather than letting PHP do it automatically.
            $compressedContent = gzencode($content, 9, FORCE_GZIP);
            $compressedContentLength = strlen($compressedContent);
            if ($compressedContentLength/$contentLength < 0.9) {
                if (ini_get('zlib.output_compression')) ini_set('zlib.output_compression', false);
                $response->header('Content-Encoding', 'gzip');
                $response->setContent($compressedContent);
                $contentLength = $compressedContentLength;
            }
        }

        // compressed or not, sets the Content-Length           
        $response->header('Content-Length', $contentLength);
        $response->header('Accept-Ranges', 'bytes');

        return $response;
    }
}

Here is my request header:

Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Cookie:PHPSESSID=oejhga8anp56bhjhad1ru79uv3; XSRF-TOKEN=eyJpdiI6IkhUOEVYSzFwZkY0ejcrSWlkM2hBa3c9PSIsInZhbHVlIjoicmUwOWc5TXhnMWZRa3B6eGU5RGk1bE1YTGd0UjUyQ05XNkhuNzczUGRyam4rU2dvVlwvVjRVbzJNMjlQN3NoaENaeGVZYitFZGlIQ2JqU21uWmppK0tnPT0iLCJtYWMiOiJmNDNmMDU1YjdhZDIwNGU1NTczMjUyOGY5NzY1NDg1YTE1MDNkMDM3OGI4ZmI2OGRjZWI3MDZiYmJmNjBiMjM1In0%3D; laravel_session=eyJpdiI6Ik5yNXZJN2sxQzJUanhzeFlUYTlHXC9nPT0iLCJ2YWx1ZSI6IkJsUjNxeXBuOVRCMDg5TUIrR3pVRUg3VndpYkk1dExNZUF6OFYzNnJBejlrRmdrTTF4cXRmV015YXpnWXpGSlFBRngwZytrS3FNWU95ZktOOVBSU0VBPT0iLCJtYWMiOiJhMzFjYmRkNThlYmIyYmRmZGEzNjgwYTBjYWFkOWNhMDNiNWM3YmQ0MDg3MDQ2MmFiNTllODZjZWM2MWVkNWEzIn0%3D
Host:localhost:8000
Referer:http://localhost:8000/admin/contacts
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36
X-CSRF-TOKEN:grl8Oi6D0IctxietY3nmwOlpNOQZR9fmkayxof2s
X-Requested-With:XMLHttpRequest

And Here is my response Header:

Accept-Ranges:bytes
Cache-Control:no-cache
Connection:close
Content-Encoding:gzip
Content-Length:2042
Content-Type:application/json
Date:Sun, 25 Sep 2016 21:37:56 GMT
Host:localhost:8000
Set-Cookie:XSRF-TOKEN=eyJpdiI6IndudDhkZStKc3Z2NTREWFZjWndxcFE9PSIsInZhbHVlIjoib05Ec25GekZCZmtEcXV0WDNQb3BYcVBLbDZaVEhQQjhmNmY4SzlqMjdFdkNxZGU2cUowUnA0SXZvU29pTm94RklMaXRSZzdcL3pNbmp0S3RKVnFMQjFRPT0iLCJtYWMiOiI1ZTRkMjAzM2RiZDdiYWFkZTNkNjgwYWZkNzY1NzkxZjY0NTQ5NmRkODM4YWUwYWZhZjkyMjg4OWM1ZWQzNzY4In0%3D; expires=Sun, 25-Sep-2016 23:37:56 GMT; Max-Age=7200; path=/
Set-Cookie:laravel_session=eyJpdiI6IlFDTXFBbGF6T2U1UnlMbUJCSUNpS2c9PSIsInZhbHVlIjoibkRGNUlKYURtemxXSTUxVkdnMmdzVXNCS1oxU1haM3ZzUm43dFlzWnUyQXByNStadURqVXRHWXNMTWUzbDRMY3hwYXBsYkhrZzI5bVJhaTZOVGNNTHc9PSIsIm1hYyI6ImZmMGM1NzVhOGVhMTc1MTVhZGQ3NDEwY2Q5YzkxNGJmYTE2ODJjMmFhNTgzYjA3YWMyMDBhZDEwMzkwMzhmZGQifQ%3D%3D; expires=Sun, 25-Sep-2016 23:37:56 GMT; Max-Age=7200; path=/; httponly
X-Powered-By:PHP/5.5.12



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire