如何从 Laravel 控制器编写 Javascript 警报?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48403072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:17:04  来源:igfitidea点击:

How to Write Javascript Alert from Laravel Controller?

javascriptlaravel

提问by Dharmik Unagar

here, this code inside laravel controller

在这里,laravel 控制器中的这段代码

this code define from controller and use to javascript

此代码从控制器定义并用于 javascript

<script type="text/javascript">alert("hello!");</script>

it will occure error

它会发生错误

Unexpected token < in JSON at position 0

JSON 中位置 0 的意外标记 <

回答by Arshad Islam

Try:

尝试:

echo "<script>";
echo "alert('hello');";
echo "</script>";

Or try this:

或者试试这个:

return redirect()->back()->with('alert','hello');

回答by Parveen Chauhan

@push('stylesheets')
    <link rel="stylesheet" href="custom/css/required/by/child">
@endpush

@push('scripts')
    <script src="custom/js/required/by/child"></script>
@endpush

回答by ramin ashrafimanesh

return your script in controller method or create simple blade and use view function and put your script into blade:

在控制器方法中返回您的脚本或创建简单的刀片并使用视图功能并将您的脚本放入刀片:

class TestController extends Controller
    {
    public function index(){

        return '<script type="text/javascript">alert("hello!");</script>';
    }
}