我有一个关于Mootools中的Ajax调用的小问题。我在Jquery中使用AJAX编写了一个处理程序脚本和一个侦听器脚本,但我不知道如何将其转换为mootools而不是AJAX。
函数使用html链接标签onclick='changePage($page, $total)'调用这是原始的JQuery Ajax调用。它在Ajax文件中以数组的形式返回。返回的变量如下:
$data = array();
$data['result'] = '1';
$data['html'] = $this->result; //Returns Pagination bar with current selected page.
$data['html2'] = $this->result2; //Returns a block of HTML (result of Solr request styled with bootstrap
JQuery版本的脚本
function changePage(page, total) {
$.ajax({
url: '/public/ajax.php?do=getPageView',
type: 'POST',
dataType: 'json',
data: {
page: page,
total: total,
//type : type
},
success: function(data) {
//console.log(data.result);
if (data.result == 1) {
$('#placeHolder').html(data.html);
$('#vrouwen').html(data.html2);
} else if (data.status == 2) {
//do nothing :) - nothing has been input
} else {
alert("Fout!!'n" + textStatus + "'n" + errorThrown);
}
},
error: function error(jqXHR, textStatus, errorThrown) {
alert("Fout!!'n"
+ textStatus + "'n" + errorThrown);
}
});
};
我假定的Mootools版本的脚本
function changePage(page, total) {
var ajax = new Request({
async: false,
url: '/public/ajax.php?do=getPageView',
method: 'POST',
dataType: 'json',
data: {
'page': page,
'total': total,
//type : type
},
onSuccess: function(data) {
//console.log(data.result);
if (data.result == 1) {
$('placeHolder').set('html', data.html);
$('vrouwen').set('html', data.html2);
} else if (data.status == 2) {
//do nothing :) - nothing has been input
} else {
//alert("Fout!!'n" + textStatus + "'n" + errorThrown);
}
}
});
ajax.send();
};
在视图中,我有一个名为占位符的div,这是分页栏的位置。html2被插入到id='vrouwen'的div中。
希望你们能帮我解决这个问题。
——编辑我是在和几个程序员同事进行头脑风暴后想出这个办法的。把调查结果贴在这里供大家查看。
区别在于Jquery和Mootools处理返回值的方式。
显然JQuery处理返回值作为JSON对象当你设置dataType: ' JSON '。Mootools没有这样做,所以我在onSuccess函数中添加了以下内容:
onSuccess: function(data) {
data = JSON.decode(data);
//console.log(data.html2);
if (data.result == 1) {
$('placeHolder').set('html', data.html);
$('vrouwen').set('html', data.html2);
} else if (data.status == 2) {
//do nothing :) - nothing has been input
} else {
//alert("Fout!!'n" + textStatus + "'n" + errorThrown);
}
}
现在它正确地替换了div
Mootool Request is An XMLHttpRequest Wrapper
onSuccess
attached Method在请求成功完成时被触发。
返回给附加处理程序的参数是responseText
&responseXML
responseText - (string)请求返回的文本。responseXML - (mixed)来自请求的响应XML。
请求。JSON -自动接收打包请求JSON格式的JavaScript对象。意味着它是为json请求制作的当它接收到数据时,它将其转换为json对象。
这里 onSuccess
请求完成时触发。覆盖请求成功事件的签名。
返回给附加处理程序的参数是responseJSON
&responseText
responseJSON - (object)远程请求的JSON响应对象。responseText - (string) JSON响应为字符串