laravel-5.7 - 我无法从 jQuery Ajax 提交 Laravel 5.7 获取数据
问题描述
帮助我,我正在尝试在 laravel 5.7 中通过邮寄方式接收来自 jQuery ajax 提交的数据。但是我做不到TT。我总是在控制器中一无所获。我也尝试在 laravel 文档和其他网站上找到它。但我找不到任何解决方案。这是我的代码。
- 路线(web.php)
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\OrderController;
Route::get('ajax', function(){
return view("wash");
});
Route::post('getmsg', 'AjaxController@index');
- 路线(web.php)
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\OrderController;
Route::get('ajax', function(){
return view("wash");
});
Route::post('getmsg', 'AjaxController@index');
- 控制器(AjaxController.php)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
class AjaxController extends Controller{
public function index(Request $request)
{
$id = $request->input('id');
$msg = "This is a simple message ".$id;
return response()->json(array("msg"=>$msg), 200);
}
}
- 查看 (wash.blade.php)
<html>
<head>
<title>Ajax Example</title>
<meta name="csrf-token" content="{{ csrf_token() }}">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</script>
<script>
function getMessage() {
var dataString = {
id : $(this).attr('id')
};
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
type: "POST",
url: "{{ URL::to('getmsg') }}",
data: dataString,
dataType: "json",
cache : false,
success: function(data){
$("#msg").html(data.msg);
}
});
}
</script>
</head>
<body>
<div id = 'msg'>
<p>This message will be replaced using Ajax.</p>
<p>Click the button to replace the message.</p>
</div>
<?php
echo Form::button('Replace Message',['onClick'=>'getMessage()']);
?>
</body>
</html>
解决方案
推荐阅读
- java - Android - 自定义 Toast 类
- spring-reactor - 完成另一种助焊剂后如何启动一种助焊剂?
- jquery - Bootstrap 4 modal 没有在我的 vue 项目中打开
- css - 使用 CSS 在按钮中设置首字母大写
- python - 以 .txt 文件上传股票价格数据并在 python 中分析
- swift - 如何处理 Process.run 抛出的异常
- javascript - 使用 JsRender 按列创建 GRID,使用数学辅助函数
- vba - MS Access 数据库关闭,忽略表单关闭取消 true
- laravel - Laravel:无需猜测即可诊断“未捕获的反射:类日志不存在”的方法?
- sql-server - 如何解决 Pandas 中“远程过程调用协议流中的传入表格数据流不正确”的错误