You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel中AJAX未传数据问题:query1不在Request且未发送JSON

解决Laravel中AJAX数据传递异常问题

Hey, let's break down and fix this AJAX data issue you're facing in your Laravel project step by step:

核心问题分析

From your code, I can spot two main issues causing the data not to pass correctly:

  1. Parameter name mismatch: In your JS code, you're sending a parameter named query, but your controller is trying to fetch query1—that's why the value is missing in the Request instance.
  2. JSON format confusion: Your current AJAX request uses the default application/x-www-form-urlencoded content type, not JSON. If your backend expects JSON, you need to adjust the request settings.

Step 1: Fix the Parameter Name Mismatch

First, align the parameter name between your frontend and backend. Pick one name (either query or query1) and use it consistently:

Modified JS Code (fixing parameter name)

$('#Course_name').keyup(function(){ 
    var query1 = $(this).val(); 
    if(query1 != '') {
        console.log(query1); 
        $.ajax({ 
            url: '/getCourses', 
            type: "POST", 
            // Changed parameter key to `query1` to match controller's expected name
            data: {query1: query1, _token: $('input[name=_token]').val()}, 
            success: function(data){ 
                $("#course_list").show(); 
                $("#course_list").html(data.output); 
                $("#Course_name").css("background","#FFF"); 
                console.log(data); 
            } 
        }); 
    } 
});

Modified Controller Code

Now your controller can correctly fetch the value using the matched parameter name:

use Illuminate\Http\Request;

public function getCourses(Request $req)
{
    // Now this will correctly retrieve the value sent from AJAX
    $query = $req->input('query1');
    
    // Your processing logic here...
    $output = // Generate your desired output
    
    // Return response as JSON (standard for AJAX interactions)
    return response()->json(['output' => $output]);
}

Step 2: Send JSON Format Data (Optional)

If you specifically need to send JSON-formatted data (instead of the default form encoding), adjust your AJAX request with these changes:

JS Code for JSON Request

$('#Course_name').keyup(function(){ 
    var query1 = $(this).val(); 
    if(query1 != '') {
        console.log(query1); 
        $.ajax({ 
            url: '/getCourses', 
            type: "POST", 
            contentType: 'application/json', // Tell server we're sending JSON
            data: JSON.stringify({query1: query1, _token: $('input[name=_token]').val()}), // Convert data to JSON string
            success: function(data){ 
                $("#course_list").show(); 
                $("#course_list").html(data.output); 
                $("#Course_name").css("background","#FFF"); 
                console.log(data); 
            } 
        }); 
    } 
});

Controller for JSON Request

Laravel can still handle this smoothly with the input() method, but you can also use json() explicitly:

public function getCourses(Request $req)
{
    // Option 1: Auto-detects form or JSON data
    $query = $req->input('query1');
    
    // Option 2: Explicitly fetch from JSON request body
    // $query = $req->json('query1');
    
    // Your processing logic...
    return response()->json(['output' => $output]);
}

Key Takeaways

  • Always double-check that parameter keys match exactly between frontend and backend—this is the most common cause of missing data.
  • Use contentType: 'application/json' and JSON.stringify() only if you need to send JSON; otherwise, the default form encoding works perfectly for most Laravel AJAX use cases.

内容的提问来源于stack exchange,提问作者Yogesh.galav

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:15:49