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.

  • 写回答

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);
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化