Javascript 未捕获异常:Ajax 进程内存不足
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29291952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
uncaught exception: out of memory in Ajax Process
提问by Jerielle
I got a problem I am submitting a simple form that has a small data and when I checked in the consoletab 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 weird error:
我遇到了一个问题,我正在提交一个包含小数据的简单表单,当我在console选项卡中检查时,ajax 的 URL 似乎正在工作,但是在处理 ajax 后,它会发出错误警报,并将其重定向到我的主页并从控制台选项卡我有这个weird error:
Uncaught exception: out of memory
未捕获的异常:内存不足
In my ajax I have this simple code only:
在我的 ajax 中,我只有这个简单的代码:
$("#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
在我的 PHP 控制器中,我有这个功能
public function writeSubComment() {
$json = array();
$json['test'] = $this->request->post['text'];
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
}
回答by Drakes
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.
根据您对被重定向到主页的描述,但在 ajax 响应部分中没有执行此操作的代码,我怀疑元素 #add-comment 是表单中的提交按钮。
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.
如果是这种情况,那么当您单击 #add-comment 提交按钮时,您的表单可能会在 ajax 代码运行的同时提交。这将解释内存不足错误,因为在页面重定向时 ajax javascript 正在被清除。
You would need to prevent your form from submitting, and let the success() or failure sections handle the next step (i.e. refreshing the page, updating the comments box). One way to do this would be to change
您需要阻止表单提交,并让 success() 或 failure 部分处理下一步(即刷新页面、更新评论框)。一种方法是改变
$("#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;">
This is my best guess from the information I have available.
这是我从现有信息中最好的猜测。
回答by George Albertyn
I was having a similar problem while testing in Firefox. Changing the button's typefrom submitto buttonworked for me.
我在 Firefox 中测试时遇到了类似的问题。更改按钮是type从submit到button为我工作。
<button type="submit" ...>Add comment</button>
to
到
<button type="button" ...>Add comment</button>
回答by Arthur Danko
I have had this error in the implementation of a search via Ajax, when I tried to display the result in HTML DOM and solved the problem as follows:
我在通过 Ajax 执行搜索时遇到了这个错误,当我尝试在 HTML DOM 中显示结果并解决问题如下:
$('..a place where the processed output Ajax request..').on('click', '#add-comment', function() {...});
$('..a place where the processed output Ajax request..').on('click', '#add-comment', function() {...});
回答by Md. Shafiqur Rahman
my problem solved by changing
我的问题通过改变解决了
<button onClick="save_order_info_into_database()">Save</button>
into
进入
<button type="button" onClick="save_order_info_into_database()">Save</button>
means by adding type="button"solved mine..
意味着通过添加type="button"解决我的..

