N
N
Nikita2019-07-11 14:15:10
PHP
Nikita, 2019-07-11 14:15:10

Ajax ( json ) throws error about unknown character, how to get only response from php without tags?

Here is the ajax itself:

var filters_form = jQuery('#filters_form');
    var filter_search_form = jQuery('#filter__search-form');
    var form_handler_url = filters_form.attr('action');
    var form_method = filters_form.attr('method');
    var form_data = jQuery('input', filters_form).serialize();
    var data_type = 'json';

    jQuery('input', filters_form).on('change', function(){

        jQuery.ajax({
            type: form_method,
            url: form_handler_url,
            data: form_data,
            dataType: data_type,
            success: function ( response ){
                var responseData = JSON.parse(response);
    
                console.log(responseData);
            },
            error: function ( xhr, ajaxOptions, thrownError ) {
                console.log('Sorry, you have an error in you require!');
                console.log(thrownError);
                console.warn(xhr.responseText);
            }
        });
    });


Here is the handler ( function.php ): I get an error - SyntaxError: Unexpected token < in JSON at position 0 This is an error about opening php tag. How to get only the response, without the content of the file itself. this is what I get when accessing the file (console - console.warn(xhr.responseText); ):

return json_encode('Hello');



<?php return json_encode('Hello');?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey, 2019-07-11
@JohnEkler

You were given the source code of the php script, and not the result of the execution. Set up the host correctly

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question