Javascript 如何将ajax响应值传递到另一个页面?

Javascript 如何将ajax响应值传递到另一个页面?,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我正在尝试向PHP文件发送AJAX响应。根据回复,我想重定向到另一个页面。如果响应是success,我想将参数数据重定向到“谢谢”页面 如何将此transactionId传递到其他页面 if ($result->success) { print_r("success!: " .$result->transaction->id); } else if ($result->transaction) { print_r("Error processing t

我正在尝试向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')。提交(函数(){
var serializedData=$(this).serialize();
$.post('card.php',序列化数据,函数(响应){
//将响应记录到控制台
console.log(“响应:+响应”);
警报(响应);
如果(响应=“成功”){
window.location.href=http://www.google.com';
}
});
});
});

查看您的php代码-它永远不会返回
success
,它将返回
success!:有一件事
所以这个条件
if(response==“success”)
从来都不是真的

你可以用

if(response.indexOf("success") == 0)

或者更改php文件中的响应

您可以通过将响应变量合并到重定向的查询中来发送所需的响应变量

例如,如果您想将
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']

从更改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());
}

并将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);
        }
    });
});
});

window.location.href=http://www.google.com?id=“+id+”类似的内容我只收到警报。我想重定向到另一个页面。如何传递?控制台.log的输出是什么(“响应:”+Response)
?如果
card.php
返回json会更容易,因为您希望检查状态并获取ID。那么ajax回调中的响应变量将是一个正确的对象,而不仅仅是纯文本(需要手动解析才能获取ID)。响应:成功!:q3a2xptvyes我更改了响应,但它没有重定向。它仍然在同一页
console.log(“响应:+response”)之后控制台说了什么?您可能需要修剪字符串,或者选择“断开”,并将条件更改为
if(response.indexOf(“success”)>-1)
精确,但也可以在
ajax
响应中添加其他两个条件(表示是否发生错误)。请再加上这个+1我已经按照您的要求编辑了我的答案,以便在ajax中添加else条件。因此,如果请求失败,它将显示操作失败的警报消息,您的响应文本和代码将在控制台中打印。@creative:try,
window.location.replace('http://www.google.com?id=“+data.id)而不是
window.location.href=http://www.google.com?id='+data.id不客气。在我的回答中也更新了此更改,以备将来参考。@AshrafulIslamTushar其仅在chrome中工作。其他浏览器不支持此js.plz帮助
$('#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);
        }
    });
});
});