繁体   English   中英

AJAX / PHP-从POST获取特定数据(传递数据)

[英]AJAX / PHP - Get specific data back from POST (Pass data)

我有一个像这样的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"){} 

码?

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'){

    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM