Laravel Ajax 调用控制器中的函数

2023-10-31php开发问题
9

本文介绍了Laravel Ajax 调用控制器中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我是 laravel 的新手,我想对用控制器编写的函数进行 ajax 调用.我做了以下但没有工作.

在视图中:

$.ajax({类型:POST",url: 'OrderData',//不确定在此处添加什么作为 URL数据:{ id:7 }}).done(函数(味精){警报(味精);});

我的控制器位于 app/controllers/DashBoardController.php在 DashBoardController.php 里面我有

class DashBoardController 扩展 BaseController {公共函数 DashView(){返回视图::make('dashboard');}public function OrderData(){//这是我想从 ajax 调用的函数返回我在";}}

我的问题是如何从页面加载视图到我的 DashBoardController.php 中的函数进行 ajax 调用?谢谢.

解决方案

在你的 routes.php 文件中添加

Route::post('/orderdata', 'DashBoardController@OrderData');

然后使用您的 ajax 调用将数据发送到 /orderdata 数据将传递到 DashBoardController 中的 OrderData 方法p>

所以你的 ajax 调用会变成

$.ajax({类型:POST",url: '/orderdata',//这是我更新的数据:{ id:7 }}).done(函数(味精){警报(味精);});

如果你想访问数据,你需要像这样将它添加到你的方法中

class DashBoardController 扩展 BaseController {公共函数 DashView(){返回视图::make('dashboard');}public function OrderData($postData){//这是我想从 ajax 调用的函数//用那个帖子数据做一些很棒的事情返回我在";}}

并更新您的路线

Route::post('/orderdata/{postdata}', 'DashBoardController@OrderData')

I am new to laravel and I want to make an ajax call to a function written in controller. I have done the following but not working.

In View :

$.ajax({
    type: "POST",
    url: 'OrderData', // Not sure what to add as URL here
    data: { id: 7 }
}).done(function( msg ) {
    alert( msg );
});

My Controller which is located inside app/controllers/DashBoardController.php and inside DashBoardController.php I have

class DashBoardController extends BaseController {
    public function DashView(){
        return View::make('dashboard');
    }

    public function OrderData(){ // This is the function which I want to call from ajax
        return "I am in";
    }
}

My Question is how can I make an ajax call from view on page load to a function inside my DashBoardController.php ?? Thanks.

解决方案

In your routes.php file add

Route::post('/orderdata', 'DashBoardController@OrderData');

Then use your ajax call to send data to /orderdata the data will be passed through to your OrderData method in the DashBoardController

So your ajax call would become

$.ajax({
    type: "POST",
    url: '/orderdata', // This is what I have updated
    data: { id: 7 }
}).done(function( msg ) {
    alert( msg );
});

If you want to access the data you will need to add that into your method like so

class DashBoardController extends BaseController {
    public function DashView(){
        return View::make('dashboard');
    }

    public function OrderData($postData){ // This is the function which I want to call from ajax
        //do something awesome with that post data 
        return "I am in";
    }
}

And update your route to

Route::post('/orderdata/{postdata}', 'DashBoardController@OrderData')

这篇关于Laravel Ajax 调用控制器中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17