Laravel ajax请求控制器

时间:2016-10-17 19:43:19

标签: php jquery ajax laravel

您好我的ajax请求得到了奇怪的答案。这是我的控制器方法返回一个id:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

//use App\Http\Requests;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

use App\Source;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Response; //para user input post ajax


class RelationshipController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }
    public function getUserId()
    {
        return "123";
    }
}

这是我的路线:

Route::group(['middleware' => ['web']], function () {

    Route::post('/requestId', 'RelationshipController@getUserId');

});

Route::get('/home', 'HomeController@index');
Route::resource('relationship', 'RelationshipController');

这是我的ajax请求:

$("#consulta").click(function(){
    $.ajax({
        url: "requestId",
        type: 'post',
        data: {_token: CSRF_TOKEN, 'email': 'hihi'},
        success: function (data) {
            console.log(data);
        }, error: function () {
            alert("error!!!!");
        }
    });

});

我不知道为什么我会得到这个答案:

Object {message: ""}
message:""
__proto__:Object

这里有什么问题?路线组外的路线也不起作用。我在这里顺便说一句:

Route::get('/createrelationship2/{og}/{dest}/{xchange}', 'RelationshipController@createrelationship2');

如果我在&#34; / public / createrelationship&#34;如果我在&#34; / public / createrelationship2 / 10/5/1&#34; ajax调用不起作用。我不明白..

1 个答案:

答案 0 :(得分:0)

您使用的laravel版本是什么?您可以尝试替换 ['middleware'=&gt; ['web'] ['middleware'=&gt; [ 'API']

Route::group(['middleware' => ['api']], function () {

    Route::post('/requestId', 'RelationshipController@getUserId');

});