将返回值从php传递给js [英] Pass a return value from php to js
问题描述
我有3个文件main.php,action.js和ajax.php,我成功地将一些div的内容从main.php更改为一些ajax.php,并在我的javascript文件中调用了ajax。它看起来像这样:
I have 3 files main.php, action.js and ajax.php and i successfully changed the content on click of some divs from main.php to some of ajax.php with a ajax call in my javascript file. It looks like this:
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?或其他什么?
Now i need a return value from a function in ajax.php in my action.js because I want to iterate till this value (see the code above). How to pass this value from the ajax.php to the action.js. I am confused what do I need to get the value ajax?, json? or something else?
谢谢。
推荐答案
成功函数,响应,是你从PHP中获得的。所以发送JSON最简单,因为那时你的响应只是一个对象。
in the success function, response, is what you get back from the PHP. so sending JSON would be easiest, because then your response is just an object.
让我们说ajax.php会返回此JSON
Lets say ajax.php returns this JSON
{
"newtextcaption": "whatever the text is",
"boxhtml": "whatever your box html is",
"AjaxValue": 4389489473289
}
那么你的成功函数应该是
then your success function should be
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;
}
这篇关于将返回值从php传递给js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!