2

I'm trying to receive data from a form through AJAX on Laravel 5.

JavaScript code:

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:

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

Controller:

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);
}

In the network tab I can see how the parameters look like:

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"}

I also tried to access the data with $request->input('name of the field') but then it's always empty.

Does anybody have an idea what i'm doing wrong?

Bogdan
  • 43,166
  • 12
  • 128
  • 129
DDL
  • 23
  • 1
  • 1
  • 4

4 Answers4

1

The problem is that you are calling $("#newCustomerForm").serialize(), and this method serializes the form in url-encoded parameters and not a json encoded body.

In this question an answer is provided for this to work.

Community
  • 1
  • 1
Davidonium
  • 86
  • 1
  • 4
0

You can access like this

$request['name of field'];
paranoid
  • 6,799
  • 19
  • 49
  • 86
0

i think you need to receive the data in the controller as json:


    $request->json('field_of_interest')

0

The problem is your formData variable. Instead of:

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

it should be

var formData=$("#newCustomerForm").serialize();
svin
  • 149
  • 1
  • 3