gpt4 book ai didi

php - 使用 Jquery Ajax 后 mootools AJAX 出现问题

转载 作者:行者123 更新时间:2023-12-01 04:49:20 24 4
gpt4 key购买 nike

我有一个关于 Mootools 中 Ajax 调用的小问题。我使用 AJAX 在 Jquery 中编写了一个处理程序脚本和一个监听器脚本,但我不知道如何转换它,以便它可以与 mootools 而不是 Ajax 一起使用。

使用带有 onclick='changePage($page, $total)' 的 html 链接标记调用该函数这是原始的 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();
};

在 View 中,我有一个名为占位符的 div,这就是分页栏所在的位置。并且 html2 被插入到 id='vrouwen' 的 div 中。

希望大家能帮我解决这个问题。

--编辑---在与一些程序员同事集思广益后想出了这个办法。将调查结果发布在这里供所有人查看。

区别在于 Jquery 和 Mootools 处理返回值的方式。

当您设置 dataType: 'json' 时,JQuery 显然会将返回值处理为 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 请求成功完成时会触发附加方法。

返回到附加处理程序的参数是 responseTextresponseXML

responseText - (字符串)从请求返回的文本。responseXML -(混合)来自请求的响应 XML。

Request.JSON - Wrapped Request with automated receiving of JavaScript Objects in JSON Format. Means it is made for json requests and when it receives data it converts it to json object.

此处 onSuccess 请求完成时触发。 这会覆盖请求成功事件的签名。

返回到附加处理程序的参数是 responseJSONresponseText

responseJSON - (对象)来自远程请求的 JSON 响应对象。responseText - (字符串)字符串形式的 JSON 响应。

关于php - 使用 Jquery Ajax 后 mootools AJAX 出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23512930/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com