0
votes

I have very siple application. I sending a simple name and I want to get an autocomplete.

For this I using the plugin jquery autocomplete.

When I sending text (Привет мир!), I get - Привет РјРёСЂ

I don't understand what wrong.

All files have encoding - utf-8.
In html set tag - meta charset="utf-8"
In script - encodeURIComponent(request.name)

Request Headers:
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Accept-Encoding: gzip, deflate, br
Accept-Language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7

Response Headers:
Content-Type: text/html; charset=UTF-8
Content-Encoding: br

JS:

    $('input[name=\'search_name\']').autocomplete({
    'autoFocus': true,
    'source': function(request, response) {
        $.ajax({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
            },
            url: 'search/autocomplete',
            type: 'POST',
            dataType: 'json',
            data: {'filter_name':encodeURIComponent(request.name)},
            success: function(json) {
                response($.map(json, function(item) {
                    return {
                        label: item['name'],
                        value: item['client_id']
                    }
                }));
            }
        });
    },
    'select': function(event, obj) {
        addValue('addClient','client_id',obj.item.value,obj.item.label);
    }
});

PHP:

public function autocomplete(Request $request)
{
    $json = [];
    if ($request->has('filter_client')) {

        $client_info = \App\Models\Client::getClientByName($request->filter_client);
        if ($client_info) {
            foreach ($client_info as $client) {
                $json[] = [
                    'client_id' => $client->client_id,
                    'name' => $client->name,
                ];
            }
            unset($client_info);
        }
    }
    return response()->json($json);
}
1

1 Answers

0
votes

If we use next js function encodeURIComponent() in php we need use next function for stable work script - urldecode().