我是 laravel 的新手。我想使用 ajax 调用使用 laravel 创建一个 api。但我请求 ajax 调用,url 显示无效的服务器路径。
这是我的代码
我的路线文件:
Route::get("a/b","AController@c");
我的js文件:
var base = "public/index.php";
var url = base + "a/b";
$.ajax({
url : url,
dataType: "json",
timeout: 10000,
error:function(){ alert("Error getting from server") }
}).done(function(resp){
});
假设我在以下网址中:
domain.com/dev/lar/public/index.php/c/d
然后我调用这个 ajax ,然后 url 将重定向到
domain.com/dev/lar/public/index.php/c/public/index.php/a/b
这里 lar 是我的 laravel 应用程序文件夹
** Note I am using NGINX server. My Server Admin do not rewrite this url. That's why I use public/index.php **
最佳答案
Controller 声明中缺少您。首先你必须使用反斜杠。例如
Route::get("/a/b","AController@c");
在你的 ajax 代码中 url 应该是
var base = "domain.com/dev/lar/public/index.php";
var url = base + "/a/b";
关于javascript - Laravel Ajax 调用 url 到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41787243/