提问者:小点点

laravel ajax请求控制器


嗨,我从我的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');

如果我在“/public/createrelationship2/10/5/1”上,则ajax调用有效如果我在“/public/createrelationship2/10/5/1”上,则ajax调用无效。我不明白。。


共1个答案

匿名用户

您使用的是什么laravel版本?您可以尝试替换['middleware'=

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

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

});