php - Laravel 中的 Ajax 无法正常工作
问题描述
我正在尝试在我的 Web 应用程序中实现简单的 Ajax 调用,不幸的是它不起作用。
html代码
<li id="decorative_items">
Decorative Items
</li>
阿贾克斯代码:
$(document).ready(function() {
var id = 2; /* This has nothing to do with the code */
$("#decorative_items").bind("click", function() {
alert("click event fired"); /*This is working*/
$.ajax({
type:'POST',
url:'/getDecorativeItems',
data: {'id' : id},
success:function(response) {
alert("foo");
}
});
});
});
路线/web.php
Route::post('/getDecorativeItems', 'ajaxController@fetchDecorativeItems');
ajaxController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Request;
class ajaxController extends Controller
{
function fetchDecorativeItems(Request $request) {
$msg = "Reached Controller";
return response()->json(array('msg'=> $msg), 200);
}
}
我已阅读相关问题的所有答案和其他在线帖子,但我无法找到错误。
注意:代码工作正常,没有语法错误,但我无法达到预期的输出。我正在寻找的预期输出是使用 Ajax 警告“foo”
提前致谢
解决方案
推荐阅读
- java - 如何在jsp的查询中使用字符串变量
- ios - 使用 Swift 将录音上传到 Firebase
- php - $_SESSION 变量在我将其设置为 null 之前为 null
- performance - LUA 表选择的性能明智
- apache-spark - 如何将一个RDD拆分成多个RDD并相互比较
- php - UnexpectedValueException laravel:协议错误
- rabbitmq - Spring Initializer - 缺少 Zipkin 依赖项?
- javascript - Gatsby JS - 多个页面拉入具有不同类别的降价文件
- spring-data-jpa - Spring Data JPA 不会生成 PDF 格式的技术文档
- php - 在某些命令后停止检查代码语法?