【问题标题】:Laravel 5 access to ajax Post DataLaravel 5 访问 ajax Post 数据
【发布时间】:2016-05-16 08:12:43
【问题描述】:

我正在尝试通过 Laravel 5 上的 AJAX 从表单接收数据。

JavaScript 代码:

event.preventDefault();     // Disable normal behaviour of the element (Form)

var formData = {
    form: $("#newCustomerForm").serialize()     // Transmit all input data of the form serialized
}

console.log(formData);      // Log to the console the Input data

$.ajax({
    type: 'post',           // POST Request
    url: 'save',            // Url of the Route (in this case user/save not only save)
    data: formData,         // Serialized Data
    dataType: 'json',       // Data Type of the Transmit
    beforeSend: function (xhr) {
        // Function needed from Laravel because of the CSRF Middleware
        var token = $('meta[name="csrf_token"]').attr('content');

        if (token) {
            return xhr.setRequestHeader('X-CSRF-TOKEN', token);
        }
    },
    success: function (data) {
        // Successfuly called the Controler

        // Check if the logic was successful or not
        if (data.status == 'success') {
            console.log('alles ok');
        } else {
            console.log(data.msg);
        }
    },
    error: function (data) {
        // Error while calling the controller (HTTP Response Code different as 200 OK
        console.log('Error:', data);
    }
});

路线:

Route::post ('user/save', 'CustomerController@createNewCustomer');

控制器:

public function createNewCustomer (Request $request)
{
    $inputArray = $request->all();

    print_r ($inputArray['form']);

    // Set JSON Response array (status = success | error)
    $response = array ('status' => 'success',
                       'msg' => 'Setting created successfully',);
    // Return JSON Response

    return response ()->json ($response);
}

在网络选项卡中,我可以看到参数的样子:

radio-inline-left=on&firstname=sdsd&private_lastname=&private_title=&private_birthdate=&private_email=&business_email=&private_phone=&business_phone=&private_mobile=&business_mobile=&brand=&business_job_title=&business_address_street=sdsd&business_address_po_box=&business_address_addon_1=&business_address_addon_2=&private_zip=&private_location=&business_address_street=&business_address_po_box=&business_address_addon_1=&business_address_addon_2=&private_zip=&private_location=&source=social_media&source=&availability=on&additional-info={"status":"success","msg":"Setting created successfully"}

我也尝试使用$request->input('name of the field') 访问数据,但它总是为空。

有人知道我做错了什么吗?

【问题讨论】:

    标签: php jquery ajax laravel laravel-5.2


    【解决方案1】:

    问题是你调用$("#newCustomerForm").serialize(),这个方法序列化了url编码参数的表单,而不是json编码的body。

    In this question 提供了答案以使其正常工作。

    【讨论】:

      【解决方案2】:

      你可以这样访问

      $request['name of field'];
      

      【讨论】:

        【解决方案3】:

        我认为您需要以 json 格式接收控制器中的数据:

        $request->json('field_of_interest')

        【讨论】:

          【解决方案4】:

          问题在于您的 formData 变量。而不是:

          var formData = {
              form: $("#newCustomerForm").serialize()     
          }
          

          应该是

          var formData=$("#newCustomerForm").serialize();
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-11-05
            • 1970-01-01
            • 2019-01-31
            相关资源
            最近更新 更多