我有一个看起来像这样的ajax帖子:(位于:post.php
)
$.ajax({
type: "POST",
url: 'prize.php',
cache: false,
beforeSend: function(req) {
req.setRequestHeader("Accept", 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8');
},
data: {sw: screen.width, sh: screen.height, saw:screen.availWidth, sah: screen.availHeight, scd: screen.colorDepth, tz: (new Date().getTimezoneOffset()), bp: sbp, hf: have_flash},
success: function (data, textStatus, xhr) {
if(data=="success"){
$('#status').text("You won: $<?php echo $data['prize'] ?>!");
}else {
$("#m_error_msg").html(data);
}
},error: function (){
}
});
以上ajax调用,发布到此页面:prize.php
看起来像这样:
if($_POST){
$data = array("data"=>"success","code"=>"100","prize"=>"$prize","type"=>"$text");
die($data['data']);
}
我的问题是..如何将$data['prize']
或$data['type']
传递给:
if(data=="success"){}
码
答案 0 :(得分:2)
将dataType:'json'
添加到$.ajax()
处理程序,以声明您希望从服务器返回json encoded
结果:
type: "POST",
url: 'prize.php',
cache: false,
dataType:'json',
然后在来自服务器的回复中,发送回json_encoded
ed数组。
echo json_encode($data);
die();
然后在你的成功函数中,让我们检查一下:
success: function(data){
if(data.data == 'success'){
}
}