Error:uncaught exception: out of memory in Ajax Process

0 votes

 I am submitting a simple form that has a small data and when I checked in the console tab the URL of ajax seems to be working but after the ajax was processed it will alert an error and it is redirected to my homepage and from the console tab I have this error:

Uncaught exception: out of memory

In my ajax I have this simple code only:

$("#add-comment").on('click', function() {

    var id = $('input[name=\'review_id\']').val();
    var customer_id = $('input[name=\'customer_id\']').val();
    var $comment = $('textarea[name=\'user_comment\']').val();

    var sample = "test";

    $.ajax({
        url: 'index.php?route=product/product/writeSubComment',
        type: 'post',
        dataType: 'json',
        data: { text: sample },
        beforeSend: function() {

        },
        success: function(data) {
            console.log(data.test);
        },
        error: function() {
            alert('ERROR!!!');
        }
    });

});

In my PHP controller I have this function

public function writeSubComment() {

    $json = array();

    $json['test'] = $this->request->post['text'];

    $this->response->addHeader('Content-Type: application/json');
    $this->response->setOutput(json_encode($json));

}

How to solve this error?

Jun 16 in PHP by kartik
• 20,980 points
64 views

1 answer to this question.

0 votes

Hello @kartik,

From your description of being redirected to your homepage, but having no code in your ajax response sections to do this, I suspect that the element #add-comment is a submit button in your form.

If this is the case, then your form may be submitting at the same time the ajax code is running when you click the #add-comment submit button. This would explain the out of memory error as the ajax javascript is being expunged while the page redirects.

You would need to prevent your form from submitting, and let the success() or failure sections handle the next step .

 One way to do this would be to change

$("#add-comment").on('click', function() {
     ... 

to

$('#add-comment').on('click', function(event){ 
    event.preventDefault();
    ...

or change the submit button from

<button type="submit" ...>Add comment</button>

to

<button type="button" ...>Add comment</button>

or change the form tag like this

<form onsubmit="return false;">

Hope this works!

answered Jun 16 by Niroj
• 44,800 points

Related Questions In PHP

0 votes
1 answer

What is the use of $_REQUEST variable in php?

Hii @kartik, The $_REQUEST variable is used to read the ...READ MORE

answered Mar 27 in PHP by Niroj
• 44,800 points
80 views
0 votes
1 answer

How to merge two arrays while keeping keys instead of reindexing in php?

Hello, Considering that you have $replaced = array('1' => ...READ MORE

answered Apr 1 in PHP by Niroj
• 44,800 points
63 views
0 votes
1 answer

Error:PDOException SQLSTATE[HY000] [2020] No such file or directory in php?

Hello, I checked php -i | grep pdo and noticed ...READ MORE

answered Apr 2 in PHP by Niroj
• 44,800 points
77 views
0 votes
1 answer

How can I handle the warning of file_get_contents() function in PHP?

Hello @kartik, This is fairly simple: if (!$data = ...READ MORE

answered Apr 7 in PHP by Niroj
• 44,800 points
271 views
0 votes
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18 in Laravel by Niroj
• 44,800 points
1,391 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18 in Laravel by Niroj
• 44,800 points
125 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23 in Laravel by Niroj
• 44,800 points
131 views
0 votes
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23 in Laravel by Niroj
• 44,800 points
1,354 views
0 votes
1 answer

Error:Jquery - Uncaught TypeError: Cannot use 'in' operator to search for '324' in

Hello @kartik, You have a JSON string, not ...READ MORE

answered Jun 16 in PHP by Niroj
• 44,800 points
211 views
0 votes
1 answer

How to post the parameter in ajax call of jquery datatable?

Hello @kartik, Just pass it like a normal ...READ MORE

answered Jun 16 in PHP by Niroj
• 44,800 points
263 views