无法识别数据变量。未捕获的ReferenceError:未定义edu_id

时间:2018-10-28 01:24:50

标签: javascript ajax

我即将使用Laravel将edu_id从ajax传递给我的控制器

锚标签

<a href="javascript:void(0);" onclick="showEditEducation(some_specific_id);" title=""><i class="la la-pencil"></i></a>

JavaScript函数

function showEditEducation($edu_id)
{
    console.log($edu_id);
     $.ajax({
        type: "POST",
        url: "{{ route('show-edit-education', $user->id) }}",
        data: {"education_id": edu_id,"_token": "{{ csrf_token() }}"},
        datatype: 'json',
        success: function (json) {
            $("#showMe").html(json.html);
        }
     });
}

Laravel控制器

 public function showEditEducationForm(Request $request, $user_id)
    {
        $user = User::find($user_id);
        $education_id = $request->input('education_id'); 

        $applicantEducation = ApplicantEducation::find($education_id);

        dd($applicantEducation);

    }

console.log输出正常。它返回一个数字。唯一的问题是ajax无法识别“ edu_id”

1 个答案:

答案 0 :(得分:0)

正如兰迪爵士所说。只需简单地加一个美元符号就可以解决问题。

edu_id

$edu_id

最终的javascript功能代码

function showEditEducation($edu_id)
{
    console.log($edu_id);
    $.ajax({
        type: "POST",
        url: "{{ route('show-edit-education', $user->id) }}",
        data: {"education_id": $edu_id,"_token": "{{ csrf_token() }}"},
        datatype: 'json',
        success: function (json) {
            $("#showMe").html(json.html);
        }
 });

}