dtn913117
2019-01-27 22:58
浏览 65

如何在我的视图中使用Javascript访问我的PHP响应?

I'm trying to use my PHP response in a view and output to the console using JavaScript. I can pass the variable $e->getResponseBody()->errors['0']->detail and use it in my view, but how do I access it in JavaScript?

// My controller
try {

} catch (\SquareConnect\ApiException $e) {
    return View::make('reservations.paymentForm')->with('e', $e)->render();
}

// My view JavaScript
$(document).ready(function (e) {
    console.log(e.detail);
});

When a user submits, and an error occurs I want to redirect them back to the form view and show error in console with JavaScript (example: CVV code incorrect). Ultimately I won't be using the console but once I get that I feel confident manipulating the rest.

图片转代码服务由CSDN问答提供 功能建议

我正在尝试在视图中使用我的PHP响应并使用JavaScript输出到控制台。 我可以传递变量 $ e-> getResponseBody() - > errors ['0'] - > detail 并在我的视图中使用它,但是如何在JavaScript中访问它? / p>

  //我的控制器
try {
 
}} catch(\ SquareConnect \ ApiException $ e){
返回查看:  :make('reservations.paymentForm') - > with('e',$ e) - > render(); 
} 
 
 //我的视图JavaScript 
 $(document).ready(function  (e){
 console.log(e.detail); 
}); 
   
 
 

当用户提交时,发生错误我想重定向 他们回到表单视图并在JavaScript控制台中显示错误(例如:CVV代码不正确)。 最终我不会使用控制台,但一旦我得到它,我就有信心操纵其余部分。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongxiangshen7916 2019-01-28 01:12
    已采纳

    After much help here is the answer. I ended up not actually using javascript in the end but my answer will show it. If you pass the variable to the view, you can use it in javascript.

    // My controller
    try {
    
    } catch (\SquareConnect\ApiException $e) {
    return View::make('reservations.paymentForm')->with('myVariable', $myVariable)->render();
    

    }

    // My view JavaScript
    $(document).ready(function () {
    var myVariable = '<?php echo $myvariable ; ?>';
    console.log(myVariable);
    });
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题