简体   繁体   English

AJAX 请求总是返回相同的(Laravel)

[英]AJAX request always return the same (Laravel)

I'm trying to obtain all data from a table named "fases".我正在尝试从名为“fases”的表中获取所有数据。

Picture图片

That's how i'm listing it.这就是我列出它的方式。 With "cards".用“卡片”。

On the button "Fase", i'm trying to obtain a specific "fase" and then, edit it.在“Fase”按钮上,我正在尝试获取特定的“fase”,然后对其进行编辑。 It works pretty well, except when i trying to edit the another "fase", distinct to the first one.它工作得很好,除非我试图编辑另一个与第一个不同的“fase”。 The controller always returns the data of the first one. controller 总是返回第一个的数据。

<a href="#" class="btn btn-outline-warning" 
                                data-toggle="modal" 
                                data-target="#modalfases" 
                                onclick="consultarFase({{$fase->id}})">
                                Fase
</a>

This is my called JavaScript function "consultarFase()"这是我称为 JavaScript function "consultarFase()"

consultarFase = (Identificador) => {
  $.ajax({
    url : window.location+"/get_editar_fase",
    async: true,
    data: {id_fase:Identificador},
    success: function(response){
      response = $.parseJSON(response);
      $('#id_fase_modal').val(response.id);
      $('#nombre_fase_modal').val(response.nombre);
      $('#descripcion_fase_modal').val(response.descripcion);
      $('#fecha_limite_fase_modal').val(response.fecha_limite);
    },
    error: function(response){
      alert("Algo salió mal... vuelve a intentarlo");
    }
  });
}

This is the code on the controller:这是 controller 上的代码:

public function getEditarFase($request){
    $fase = AlumnoDao::getFaseById($request);
    return json_encode($fase);
}

And its Dao's method:及其道法:

public static function getFaseById($id){
    $fase = DB::table('fase')
    ->where('id', $id)
    ->first();
    return $fase;
}

In web.php file, this is the route:在 web.php 文件中,这是路径:

Route::get('/alumno/proyectos/{id_proyecto}/get_editar_fase','AlumnoController@getEditarFase')->name('alumno.getEditarFase');

In "consultarFase()" i tried to print "identificador" in console.log;在“consultarFase()”中,我尝试在 console.log 中打印“identificador”; It changes.它改变。

if i want to edit the first one, it prints "1" if i want to edit the second one, it print "2" (Its ID's)如果我想编辑第一个,它会打印“1”如果我想编辑第二个,它会打印“2”(它的 ID)

But not the controller... it just doesn't works但不是 controller ......它只是不起作用

 type: "get"

Add this type method to ajax request, then check.将此类型方法添加到 ajax 请求中,然后检查。

It was the getEditarFase function.它是 getEditarFase function。 Its parameters其参数

public function getEditarFase(Request $request){
    $fase = AlumnoDao::getFaseById($request->id_fase);
    return json_encode($fase);
}

The "Request" class. “请求”class。

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

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