我正在尝试向PHP文件发送AJAX响应。基于响应,我想重定向到另一个页面。如果回复是success
,我想重定向到“谢谢你”。页面以及参数数据。
如何将此transactionId传递到另一个页面?
if ($result->success)
{
print_r("success!: " .$result->transaction->id);
}
else if ($result->transaction) {
print_r("Error processing transaction:");
print_r("\n code: " .$result->transaction->processorResponseCode);
print_r("\n text: " .$result->transaction->processorResponseText);
}
else
{
print_r("Validation errors: \n");
print_r($result->errors->deepAll());
}
$('#paymentForm').submit(function() {
var serializedData = $(this).serialize();
$.post('card.php', serializedData, function(response) {
// Log the response to the console
console.log("Response: "+response);
alert(response);
if (response == "success") {
window.location.href = 'http://www.google.com';
}
});
});
});
答案 0 :(得分:3)
从
更改您的php输出if ($result->success)
{
print_r("success!: " .$result->transaction->id);
}
else if ($result->transaction) {
print_r("Error processing transaction:");
print_r("\n code: " .$result->transaction->processorResponseCode);
print_r("\n text: " .$result->transaction->processorResponseText);
}
else
{
print_r("Validation errors: \n");
print_r($result->errors->deepAll());
}
到
if ($result->success)
{
echo json_encode(array('status'=>'success','id'=>$result->transaction->id));
}
else if ($result->transaction) {
echo json_encode(array('status'=>'error','code'=>$result->transaction->processorResponseCode, 'text'=> $result->transaction->processorResponseText)));
}
else
{
echo json_encode(array('status'=>'error','text'=>$result->errors->deepAll()));
}
并将您的js更改为以下
$('#paymentForm').submit(function() {
var serializedData = $(this).serialize();
$.post('card.php', serializedData, function(response) {
// Log the response to the console
console.log("Response: "+response);
alert(response);
var data= $.parseJSON(response);
if (data.status == "success") {
window.location.replace('http://www.google.com?id='+data.id);
}else{
alert('Operation Failed');
if(data.code){
console.log('response code: '+data.code);
}
console.log('response text: '+data.text);
}
});
});
});
答案 1 :(得分:1)
查看你的php代码 - 它永远不会返回success
,它会返回success!: somthing
所以这个条件if (response == "success")
永远不会是真的
你可以使用
if(response.indexOf("success") == 0)
或更改php文件中的响应
答案 2 :(得分:1)
您可以通过将响应变量合并到重定向的查询中来发送您想要的响应变量。
例如,如果您想将foo
传递给重定向的php domain.com/bar.php
:
var url = 'domain.com/bar.php?foo=' + foo
window.location.href = url;
然后在PHP页面上,您可以使用以下数据:
$bits = parse_url($url);
parse_str($bits['query'], $query);
echo $query['foo']