[英]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.