2017-12-18 345 views
0

我想返回一个url作为Ajax响应。但在此之前,我使用递归功能来展平保持键的多维数组。问题,同时返回ajax响应

function response(){ 
    ... 
    $response = Ezpay::PayWithToken($obj); 
    $trans_resp = json_decode(json_encode($response),true); 
    $resp_array = $this->flatten($trans_resp); 
    //saving transaction response from gateway to sessioion 
    Session::push('ezpay_gateway_resp',json_encode($resp_array)); 
    print_r(Session::get('ezpay_response')) 
    return '/gateway/success'; 
} 

和递归函数是

function flatten($array, $prefix = '') { 
     $result = array(); 
     foreach($array as $key=>$value) { 
      if(is_array($value)) { 
       $result = $result + $this->flatten($value, $key); 
      } 
      else { 
       $result[$key] = $value; 
      } 
     } 
     return $result; 
} 

'/gateway/success'

+1

在从哪儿获取'$ trans_resp'你的回应方法是什么? – linktoahref

+0

$ response = Ezpay :: PayWithToken($ obj); $ trans_resp = json_decode(json_encode($ response),true); – Shalom

+0

在'$ this-> flatten($ trans_resp)',其中''trans_resp'来自函数response()'。对不起如果我在以前的评论中不清楚 – linktoahref

回答

0

现在正在返回$result阵列istead从您的代码print_r(Session::get('ezpay_response'))

显示使用页面上的文本echo

echo '/gateway/success'; 
+0

删除。我只是改了名字,但它不是问题 – Shalom

+0

你改变了什么名字? – madalinivascu

+0

你确定你在调用response()而不是flatten()吗? – madalinivascu

0

,应该回送的网址:

function response(){ 
    $resp_array = $this->flatten($trans_resp); 
    //saving transaction response from gateway to sessioion 
    Session::push('ezpay_gateway_resp',json_encode($resp_array)); 
    echo '/gateway/success'; 
} 

这是一个完整的例子。

控制器:

namespace App\Http\Controllers; 
use Illuminate\Http\Request; 
use App\Http\Requests; 
use App\Http\Controllers\Controller; 

class ExampleController extends Controller 
{ 
    public function message() 
    { 
     $msg = "myurl/index"; 
     return response()->json(array('msg'=> $msg), 200); 
    } 
} 

路线:

Route::get('ajax',function() 
{ 
    return view('message'); 
}); 
Route::post('/msg','[email protected]'); 

的Javascript使用它:

function getMessage(){ 
    $.ajax({ 
     type:'POST', 
     url:'/msg', 
     data:'_token = <?php echo csrf_token() ?>', 
     success:function(data){ 
      $("#msg").html(data.msg); 
     } 
    }); 
}