Jquery Ajax 调用在数据库上插入新记录。被调用的文件(php)需要返回两个值,插入的内容和它的id。
我应该如何在 php 上发回这些值(数组、xml、echo)?
如何在 Jquery Ajax 成功(数据)中分离这些?
目前,处理 php 文件仅返回一个字符串,即插入的内容,然后我用该字符串更新 View 。需要返回记录 ID,以便我可以连接该元素。
如果需要更多说明,请提出建议。谢谢您
JQUERY
$.ajax({
type:'post',
url: 'posts_in.php',
data: $("#postentry").serialize(),
success:function(data){
var id='posts';
//split returned data here
$('<div></div>').attr('id',id).addClass('boxee').html(data).prependTo('#boxer');
$('#tittle_ent').val('').focus();
}
PHP
echo $value1,$value2;
print_r($arrayhere);
XML?
最佳答案
返回使用 json_encode()
编码的 JSON 字符串。
$php_array_result = array(1,2,3,4,5,6);
// Send the correct MIME header and echo out the JSON string
header("Content-type: application/json");
echo json_encode($php_array_result);
exit();
在 jQuery ajax 调用中,添加 dataType: "json"
属性。
$.ajax({
type:'post',
url: 'posts_in.php',
dataType: "json",
success: function(data) {
console.dir(data);
},
// etc...
关于php - 如何从 jQuery 调用中创建/检索数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6717962/