libertey's avatar

GEt only an empty string from ajax request

Hello, im trying to build an function, and i think it should work but my get request is always sending an empty string and not the rendered view. Does anybody have an ides where the mistake is.

My Function in the controller.

public function order($request)
    {
        $value = $request->input('value');
        $section = $request->input('section');

        switch ($section) {
            case 'mediadata':
                $categories = Category::where(Category::ATTRIBUTE_PUBLICATION_TYPE, Category::TYPE_MEDIA_DATA)
                    ->where(Category::ATTRIBUTE_IS_ONLINE, 1)->get();
                $publications = [];
                foreach ($categories as $cat) {
                    foreach ($cat->publications as $pub) {
                        $publications[] = $pub;
                    }
                }
                $publications = collect($publications)->sortBy('name');
                $letters = [];
                $publicationLetter = [];
                foreach ($publications as $publication) {
                    $letters[] = strtoupper($publication->name[0]);
                    $letters = array_unique($letters);
                    foreach ($letters as $letter) {
                        if ($letter === strtoupper($publication->name[0])) {
                            $publicationLetter[$letter][] = $publication;
                        }
                    }
                }
                $sortedArray = collect($publicationLetter)->sortKeys();
                $view = view('frontend.mediaData.sortData', compact('sortedArray'));
                break;
        }
        return $view->render();
    }

My AJAX Function

function getFilteredData(type, section) {
        let value
        switch (type) {
            case 'search':
                value = $('#searchField').val()
                break
            case 'order':
                value = $('#selectField').val()
                break
        }
        $.ajax({
            url: '{{route('filter')}}',
            method: 'get',
            data: {
                type,
                section,
                value,
            },
            success: (res) => {
                console.log(res)
                $('.filter-container').html(res)
            },
        })
    }
0 likes
15 replies
Sinnbeck's avatar

Not quite clear where you are missing data? Which part is an empty string exactly?

libertey's avatar

@Sinnbeck Hello, right here in the response

success: (res) => {
                console.log(res) // Throws out <empty string> 
                 $('.filter-container').html(res)
            },
Sinnbeck's avatar

@libertey Try opening dev tools (f12) in chrome and select network. Now do the request, and click on the request in the list. Then select the preview tab. Does that have data?

Btw. No need to call render() on the view

        return $view;
libertey's avatar

And if i render it inside the case like that

$view = view('frontend.mediaData.sortData', compact('sortedArray'))->render();
dd($view);

My HTML is rendered absolutely correct, it just does not return the HTML.

Sinnbeck's avatar

Also be sure to have a default in the switch. If $section isnt "mediadata", you never set a view? (I assume there is supposed to be multiple cases, but you havent written them yet)

libertey's avatar

@Sinnbeck yes the default is set, the other cases are the next step, and on f12 in network tab there is nothing in the preview.

Sinnbeck's avatar

@libertey Then you need to use dd() at different points in the code to see where it goes wrong. Is it correct to assume that you are not showing the complete code as there is no default case?

libertey's avatar

@Sinnbeck This is my full switch function till now

$value = $request->input('value');
        $section = $request->input('section');

        switch ($section) {
            case 'mediadata':
                $categories = Category::where(Category::ATTRIBUTE_PUBLICATION_TYPE, Category::TYPE_MEDIA_DATA)
                    ->where(Category::ATTRIBUTE_IS_ONLINE, 1)->get();
                $publications = [];
                foreach ($categories as $cat) {
                    foreach ($cat->publications as $pub) {
                        $publications[] = $pub;
                    }
                }
                $publications = collect($publications)->sortBy('name');
                $letters = [];
                $publicationLetter = [];
                foreach ($publications as $publication) {
                    $letters[] = strtoupper($publication->name[0]);
                    $letters = array_unique($letters);
                    foreach ($letters as $letter) {
                        if ($letter === strtoupper($publication->name[0])) {
                            $publicationLetter[$letter][] = $publication;
                        }
                    }
                }
                $sortedArray = collect($publicationLetter)->sortKeys();
                $view = view('frontend.mediaData.sortData', compact('sortedArray'))->render();
                dd($view); // here it returns the correct value of the view I want to render
                return $view; // it returns <empty string>
                break;
            default:
                return redirect()->back();
        }
libertey's avatar

@Sinnbeck yeah i will adjust the default one later, will build an error blade which should be rendered if section is not set... right now it will only work for section 'mediadata' there are several other things the function has to do, but first of all i try to get that thing working.

Because mediadata is set as section and it runs straight till the dd() but the return state is not rendering anything.

Sinnbeck's avatar

@libertey If you just open the url directly in a browser tab, does it work there? Rightclick the request in the list, and select "Open in new tab"

libertey's avatar

@Sinnbeck OMG I'm so sorry that i wasted your time, I forgot i had an main function, where it filters (not the best solution, must work over that) the filter type, called filter() and i forgot to put the return statement before that function...

    public function filter(Request $request)
    {
        $type = $request->input('type');

        switch ($type) {
            case 'search':
                return $this->search($request);
                break;
            case 'order':
                return $this->order($request); // i forgot to return it in here -.-
            default:
                return $this->error();
        }
    }

Please or to participate in this conversation.