bremg
bremg

Reputation: 48

Ajax post response contains html

I want to post some data to php with ajax. Then I want the response from php.

My response is showing up like I want but is followed by the entire page's HTML.

Im using Laravel. The item gets deleted, so the post seems to work.

This is the javascript, I really don't want to use jquery just because of a ajax problem..

        var request = new XMLHttpRequest();
        request.open('POST', window.location.pathname + "/delete", true);
        request.setRequestHeader("Content-Type", "application/json");
        request.setRequestHeader("X-CSRF-TOKEN", document.head.querySelector("[name=csrf-token]").content );
        request.dataType = 'text';

        var data = JSON.stringify({"itemid": itemid});

        request.send(data);

        request.onreadystatechange = function() {
            if (request.readyState === 4) {

                console.log(request.responseText);

                var response = JSON.parse(request.responseText);

                if (request.status === 200 && response.status === 'OK') {
                    console.log('successful');
                } else {
                    console.log('failed');
                }
            }
        }

The php code ends in

return "I only want this return value"

Somehow the response text is

I only want this return value<link rel='stylesheet' type='text/css' property='stylesheet' href= etc etc etc (the entire page html)

Upvotes: 0

Views: 491

Answers (1)

bremg
bremg

Reputation: 48

Found it, finally!

Is the laravel debug bar https://github.com/barryvdh/laravel-debugbar.

I added the following routes and it worked.

Route::get('/_debugbar/assets/stylesheets', [
'as' => 'debugbar-css',
'uses' => '\Barryvdh\Debugbar\Controllers\AssetController@css']);

Route::get('/_debugbar/assets/javascript', [
'as' => 'debugbar-js',
'uses' => '\Barryvdh\Debugbar\Controllers\AssetController@js']);

Upvotes: 1

Related Questions