我有 3 个文件 main.php、action.js 和 ajax.php,我通过在我的 javascript 文件中调用 ajax,成功地将一些 div 从 main.php 更改为一些 ajax.php。它看起来像这样:
var value = $(this).attr("id");
$.ajax({
type: 'get',
url: "ajax.php",
data: {
auto_value: value
},
success: function(response) {
$('.gridnr1, .textnr1').fadeOut(400, function(){
$('.textnr2, .gridnr2').fadeIn(400);
});
var newtextcaption = $(response).filter('#newtextcaption').html();
var box = $(response).filter('#box').html();
$('#textnr2').html(newtextcaption);
$('#gridnr2').html(box);
for (var i=0; i<VALUE_FROM_AJAXphp; i++) {
DO SOMETHING WITH i;
}
});
现在我需要从我的 action.js 中的 ajax.php 中的函数返回值,因为我想迭代直到这个值(参见上面的代码)。 如何将此值从 ajax.php 传递到 action.js。 我很困惑我需要什么来获取值 ajax?,json?还是别的?
谢谢。
最佳答案
在成功函数中,response 是您从 PHP 返回的内容。所以发送 JSON 是最简单的,因为那样你的响应只是一个对象。
假设 ajax.php 返回这个 JSON
{
"newtextcaption": "whatever the text is",
"boxhtml": "whatever your box html is",
"AjaxValue": 4389489473289
}
那么你的成功函数应该是
success: function(response) {
$('.gridnr1, .textnr1').fadeOut(400, function(){
$('.textnr2, .gridnr2').fadeIn(400);
});
var newtextcaption = response.newtextcaption;
var box = response.boxhtml;
$('#textnr2').html(newtextcaption);
$('#gridnr2').html(box);
for (var i=0; i<response.AjaxValue; i++) {
DO SOMETHING WITH i;
}
关于javascript - 将返回值从 php 传递给 js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39582037/