laravel5.5 ajax post,Laravel 5 access to ajax Post Data

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?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值