简体   繁体   English

在Laravel中使用Ajax,在控制台中返回404 Not Found

[英]Using Ajax in Laravel, returning 404 Not Found in console

I'm trying to send a search query and display the results via ajax. 我正在尝试发送搜索查询并通过ajax显示结果。 For some reason when I submit the search I'm getting a 404 Not Found error message in console. 由于某种原因,当我提交搜索时,我在控制台中收到404 Not Found错误消息。 Any hints or pointers would be greatly appreciated! 任何提示或指针将不胜感激!

Update: just to clarify the URL is not foo/local/bar/... , I was just keeping that stuff private because I'm paranoid lol. 更新:只是为了澄清URL不是foo / local / bar / ...,我只是将这些内容保密,因为我很偏执。 Route was asked for, here is the only route involved: 询问了路线,这是唯一涉及的路线:

/*Home Page*/
    Route::get('/', array(
    'as'    => 'home',
    'uses'  => 'HomeController@home'
    ));

Here are the console error logs: 以下是控制台错误日志:

POST http://foo/local/bar/public/ 404 (Not Found) jquery.js:9597 POST http://foo/local/bar/public/ 404 (Not Found) jquery.js:9597

Object 
   {readyState: 4, getResponseHeader: function, getAllResponseHeaders: function, 
   setRequestHeader: function, overrideMimeType: function…}              home.js:137

   error                                                                 home.js:138

   Not Found                                                             home.js:139

JS: JS:

$(document).ready(function(){
    $("#search_form").submit(function(e) {
        e.preventDefault();

        //form_data
        var data = $('#search_form').serializeArray();
        $.ajax({
            url: window.location,
            type: "POST",
            data: data,
            success: function(data){
                $("#search_results").html($search);
            },
            error: function(xhr, status, error){
                console.log(xhr);
                console.log(status);
                console.log(error);
                $("search_results").html("Failure");
            }
        });
    });
});

Controller: 控制器:

public function post_index() {
        $search_table   = Input::get('search_table');
        $search_column  = Input::get('search_column');
        $search_input   = Input::get('search_input');

        $search = DB::table($search_table)->where($search_column, 'LIKE', "%{$search_input}%")->get();
        echo json_encode($search);
        exit;
    }

View: 视图:

<form id="search_form" method="POST" action="" >    
    <select id="search_table" class="search_table">
        <option value = ''>Search</option>
        <option value = 'commissions'>Search commissions</option>
    </select>   

        <div class="search_box" id="search_column_div">
            <select id='search_column' name='search_column'>
                 <option value='select'>by ...</option>
                 <option value='street_address'>by street address</option>
                        </select>
        </div>

        <div id="search_input_div">
            <input id='search_input' name='search_input' class='search_input' placeholder='Enter Street Address'/>
        </div>

<input type="submit" class="button expand radius" id="search_button"/>
</form>

I can see two problems with your code immediately. 我可以立即看到您的代码有两个问题。 The firs it that your route is listening for a GET request but you're using a POST request in your AJAX call. 首先,您的路由正在侦听GET请求,但是您在AJAX调用中使用了POST请求。 The second issue is that you've named your controller function incorrectly. 第二个问题是您对控制器功能的命名不正确。 You're using the older Laravel 3 syntax but you've tagged Laravel 4 in the question. 您正在使用较旧的Laravel 3语法,但已在问题中标记了Laravel 4。 Using Laravel 4 RESTful controller function naming conventions, your function name should be postIndex() 使用Laravel 4 RESTful控制器函数命名约定,您的函数名称应为postIndex()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM