问题描述
我是全新的,以Laravel 4,看我是否可以将我的网站上,它(previously没有写一个框架)。我不能让AJAX(通过jQuery)与我的控制器正常互动。
I am brand new to Laravel 4 and seeing if I can convert my website over to it (previously written without a framework). I can't get AJAX (via jQuery) to interact properly with my controller.
所以,首先,我的工作在控制器中被称为 IndexController.php
键,有一个名为类型的函数
这如下所示:
So firstly, the controller I am working in is called IndexController.php
and has a function called types
which is shown below:
class IndexController extends BaseController {
// Other controller functions
public function types($brand) {
$types = DB::select('select * from aircraft_types where brand_id = ?', array($brand));
echo json_encode($types);
}
}
该函数返回从数据库中的数据(JSON格式)。我已经再创建该控制器的路由和功能如下:
The function returns the data from the database (in JSON format). I have then created a route to this controller and function as follows:
Route::get('types/{id}', array('uses'=>'IndexController@types'));
我已经翻倍检查了线路和功能正在努力通过将// locahost / lavarel /公/类型/ 1,事实上它返回正确的JSON数据。
I have doubled checked that the route and function are working by going to //locahost/lavarel/public/types/1 and indeed it returns the correct JSON data.
有关jQuery的功能如下:
The jquery function in question is below:
function updateTypes($brand) {
$("#type").append('<option value="test">Test...</option>'); // This executes correctly
$.getJSON('/types/'+$brand, function(data) {
$("#type").append('<option value="test 2">Test 2...</option>'); // This does not
// Some other JSON related code
});
要测试,其中功能的工作,我已经插入其中编辑的项目我使用两条线。该函数被调用正确的第一个'测试'选项被追加。但是,它似乎永远不会激活回调函数作为测试code中的第二行不执行。
To test where the function works, I have inserted two lines which edit the item I am using. The function is being called correctly as the first 'Test' option is being appended. However, it never seems to activate the callback function as the second line of test code is not executed.
我怀疑的问题是我提供给JavaScript /类型/+ $品牌
的URL。我已经看到了一些教程基地变种我提供的URL,然后使用,但我不明白为什么我的code以上是行不通的。任何指针?
I suspect the issue is the URL I am providing to JavaScript '/types/'+$brand
. I have seen in some tutorials a BASE var used before the URL I provide, but I don't see why my code above wouldn't work. Any pointers?
谁能给我解释一下我要去的地方错了?
Can anyone explain to me where I am going wrong?
推荐答案
您的基本路径,以你的laravel项目 locahost / laravel /公/
但你的AJAX请求去刚本地主机
。这里是一些方法来解决这个。
Your base path to your laravel project is locahost/laravel/public/
but your AJAX-request goes to just localhost
. There're some methods for fixing this.
方法1:
这是最preffered方法。 (在我看来)
This is the most preffered method. (in my opinion)
而不是使用nginx的,阿帕奇启动Web服务器,你可以使用PHP的内置网络服务器。
Instead of using nginx, apache for starting a web server, you can use PHP's built in web server.
打开一个终端窗口(或cmd,在Windows中),cd到你的项目(一个与供应商,应用程序和公共目录)的主目录,键入命令 PHP工匠服务
。这将创建一个本地主机PHP服务器:8000,你的基本路径将是 /
Open a terminal window (or cmd in Windows), cd into the main directory for your project (the one with vendor, app and public directories) and type the command php artisan serve
. This will create a PHP-server on localhost:8000 and your base path will be /
.
有很多的选项,例如 PHP工匠的帮助服务
,如果你想看到他们所有。
There's a lot of options, for example php artisan help serve"
if you want to see them all.
当你这样做你的code应该工作。
After you've done so your code should work.
方法2
如果你想使用nginx的或Apache你应该添加一个虚拟主机为您的项目。
If you want to use nginx or apache you should add a virtualhost for your project.
这是可以做到的虽然的configs。
That can be done though the configs.
阿帕奇: http://httpd.apache.org/docs/current/虚拟主机/ examples.html
Nginx的: http://wiki.nginx.org/ServerBlockExample
之后,你应该添加新条目到主机文件。
After that you should add a new entry to the hosts file.
方法3
正如你说的,你可以在/类型/+ $品牌
。
As you said you could add a BASE variable before the '/types/'+$brand
.
您请求转到本地主机/类型/ $品牌
。
它应该去本地主机/ laravel /公/类型/ $品牌
。
只需更换/类型/'$品牌
与/ laravel /公/类型'+ $品牌
这篇关于如何使用Ajax使用jQuery在Laravel 4?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!