[英]passing callback parameters ajax post
I need some help with ajax callbacks.我需要一些有关 ajax 回调的帮助。 I'm trying to pass certain parameters after ajax is being called在调用 ajax 之后,我试图传递某些参数
var dataString = 'string=' + string;
$.ajax({
type: "POST",
url: "file.php",
data: dataString,
success: function(data){
$(".selector").html(data)
}
});
Okay so if my file.php has some mysql queries and I'm trying to echo out values好的,如果我的 file.php 有一些 mysql 查询,我正在尝试回显值
echo $picture;
echo $title;
echo $additional_values;
How do I echo out certain values and pass it through ajax如何回显某些值并将其传递给 ajax
So if I echo those out in my php file, and output it through my ajax function(data), it will output everything into $(".selector").html(data)
( <div class="selector"></div>
), however, I'm trying to output one thing at a time So if I echo those out in my php file, and output it through my ajax function(data), it will output everything into $(".selector").html(data)
( <div class="selector"></div>
),但是,我正在尝试 output 一次做一件事
success: function(data){
$(".picture").html(data); // echo's out picture in my picture div class
$(".title").html(data); // echo's out title in my title div class
$(".additional_values").html(data); // echo's out to the div class...
}
If someone can shine some light towards me, that'll be great!如果有人可以向我照射一些光,那就太好了!
Thank you!谢谢!
PHP PHP
$data = array('picture'=>$picture,
'title'=>$title,
'values'=>$additional_values,
);
print json_encode($data);
JavaScript JavaScript
var dataString = 'string=' + string;
$.ajax({
type: "POST",
url: "file.php",
data: dataString,
datatype: 'json',
success: function(data){
$(".picture").html(data.picture);
$(".title").html(data.title);
$(".additional_values").html(data.values);
}
});
You can use JSON.您可以使用 JSON。 JSON is a serialization format that allows you to "convert" an object to string an convert a string back to object. JSON 是一种序列化格式,允许您“转换” object 以将字符串转换回 object。 PHP has a built-in function to encode to JSON string, called json_encode
: PHP 有一个内置的 function 编码为 JSON 字符串,称为json_encode
:
PHP code PHP代码
file.php
<?php
//... Your logic here
header("Content-type: application/json"); //'Tell' the browser that it's a JSON response
$returnData = array('picture'=>$picture, 'title'=>$title, 'values'=>$additional_values);
echo json_encode($returnData); //Encode $returnData to JSON string
?>
Now, jQuery can receive this string and convert it to a javascript object automatically.现在,jQuery 可以接收此字符串并将其自动转换为 javascript object。 We have an object in the data
parameter我们在data
参数中有一个 object
JavaScript JavaScript
var dataString = 'string=' + string;
$.ajax({
type: "POST",
url: "file.php",
data: dataString,
success: function(data){
//At this point, data is just as php's $returnData, so it has 3 properties
//picture, title and values.
$(".picture").html(data.picture);
$(".title").html(data.title);
$(".additional_values").html(data.values);
}
});
JSON is available in many languages, and is the defacto interchange format in AJAX. JSON 有多种语言版本,是 AJAX 中事实上的交换格式。 More information on http://json.org .有关http://json.org的更多信息。
Hope this helps.希望这可以帮助。 Cheers干杯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.