如何在 Laravel 4 中使用 Ajax 和 jQuery?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17588139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:07:35  来源:igfitidea点击:

How to use Ajax with jQuery in Laravel 4?

phpajaxjquerylaravel

提问by Ben Thompson

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.

我是 Laravel 4 的新手,看看我是否可以将我的网站转换为它(以前没有框架编写)。我无法让 AJAX(通过 jQuery)与我的控制器正确交互。

So firstly, the controller I am working in is called IndexController.phpand has a function called typeswhich is shown below:

因此,首先,我正在使用的控制器被调用IndexController.php并调用了一个函数types,如下所示:

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);

}

}

The function returns the data from the database (in JSON format). I have then created a route to this controller and function as follows:

该函数返回数据库中的数据(JSON 格式)。然后我创建了一个到这个控制器的路由,功能如下:

Route::get('types/{id}', array('uses'=>'IndexController@types'));

I have doubled checked that the route and function are working by going to //localhost/lavarel/public/types/1 and indeed it returns the correct JSON data.

我通过转到 //localhost/lavarel/public/types/1 再次检查了路由和函数是否正常工作,并且确实返回了正确的 JSON 数据。

The jquery function in question is below:

有问题的jquery函数如下:

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
});

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.

为了测试函数的工作原理,我插入了两行来编辑我正在使用的项目。由于附加了第一个“测试”选项,因此正在正确调用该函数。然而,它似乎永远不会激活回调函数,因为第二行测试代码没有被执行。

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?

我怀疑问题是我提供给 JavaScript 的 URL '/types/'+$brand。我在一些教程中看到在我提供的 URL 之前使用了 BASE var,但我不明白为什么我上面的代码不起作用。任何指针?

Can anyone explain to me where I am going wrong?

谁能向我解释我哪里出错了?

回答by edenstrom

Your base path to your laravel project is localhost/laravel/public/but your AJAX-request goes to just localhost. There're some methods for fixing this.

您的 Laravel 项目的基本路径是,localhost/laravel/public/但您的 AJAX 请求只是localhost. 有一些方法可以解决这个问题。

Method 1:

方法一:

This is the most preffered method. (in my opinion)

这是最优选的方法。(在我看来)

Instead of using nginx, apache for starting a web server, you can use PHP's built in web server.

您可以使用 PHP 内置的 Web 服务器,而不是使用 nginx、apache 来启动 Web 服务器。

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 /.

打开终端窗口(或 Windows 中的 cmd),cd 进入项目的主目录(包含供应商、应用程序和公共目录的目录)并键入命令php artisan serve. 这将在 localhost:8000 上创建一个 PHP 服务器,您的基本路径将为/.

There's a lot of options, for example php artisan help serve"if you want to see them all.

有很多选项,例如,php artisan help serve"如果您想查看所有选项。

After you've done so your code should work.

完成后,您的代码应该可以工作了。

Method 2

方法二

If you want to use nginx or apache you should add a virtualhost for your project.

如果你想使用 nginx 或 apache,你应该为你的项目添加一个虚拟主机。

That can be done though the configs.

这可以通过配置来完成。

Apache: http://httpd.apache.org/docs/current/vhosts/examples.html

Apache:http: //httpd.apache.org/docs/current/vhosts/examples.html

Nginx: http://wiki.nginx.org/ServerBlockExample

Nginx:http: //wiki.nginx.org/ServerBlockExample

After that you should add a new entry to the hosts file.

之后,您应该向 hosts 文件添加一个新条目。

Method 3

方法三

As you said you could add a BASE variable before the '/types/'+$brand.

正如您所说,您可以在'/types/'+$brand.

Your request goes to localhost/types/$brand.

您的请求转到localhost/types/$brand

It should go to localhost/laravel/public/types/$brand.

它应该去localhost/laravel/public/types/$brand

Just replace '/types/'$brandwith '/laravel/public/types'+$brand

只需替换'/types/'$brand'/laravel/public/types'+$brand

回答by Andreyco

Use HTML 'base' meta tag

使用 HTML 'base' 元标记

<!doctype html>
<html lang="en">
<head>
    ...
    <base href="{{ URL::to('/') }}"/>
    ...

Declaring this tag, you ensure that every relative URL will be based on project's real, absolute URL (http://localhost/my-laravel-project/public/), not on Localhost's URL (http://localhost)

声明此标记,您可以确保每个相对 URL 都基于项目的真实绝对 URL ( http://localhost/my-laravel-project/public/),而不是基于 Localhost 的 URL ( http://localhost)