我正在尝试通过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')
访问数据,但它始终为空。
有人知道我做错了吗?
答案 0 :(得分:1)
问题是你正在调用$("#newCustomerForm").serialize()
,这个方法在url编码的参数中序列化表单而不是json编码的主体。
In this question为此提供了答案。
答案 1 :(得分:0)
您可以像这样访问
create proc sp_calcualtteSI
as
begin
DECLARE @today datetime
SET @today = dateadd(day,datediff(day,0,current_timestamp),0)
; WITH cte_dates AS
(
SELECT DISTINCT
name, Pamount, Rateofint, cdate,
CASE
WHEN ISNULL(today, @today) < DATEADD(month, 1, DATEADD(month, datediff(month, 0, cdate), 0))
THEN ISNULL(@today, @today)
ELSE
DATEADD(month, 1, dateadd(month, datediff(month, 0, cdate), 0))
END AS MonthEnd,
ISNULL(@today, @today) AS End_date
FROM
tbl_intestcalculate),
cte_Alldates AS
(
SELECT
name, Pamount, Rateofint, cdate, monthEnd, @today
FROM
cte_dates
UNION
SELECT
name, Pamount, Rateofint,
DATEADD(month, number, monthEnd),
CASE
WHEN DATEADD(month, number + 1, monthEnd) < @today
THEN DATEADD(month, number + 1, monthEnd)
ELSE @today
END,
@today
FROM
cte_dates c
CROSS JOIN
(SELECT number
FROM master..spt_values
WHERE type = 'p' AND number BETWEEN 0 AND 11) a
WHERE
dateadd(month, number, monthEnd) < @today
)
SELECT
name, cdate, monthEnd, Pamount, Rateofint,
DATEDIFF(day, cdate, monthEnd) AS No_Of_Days,
ROUND(Pamount * Rateofint * DATEDIFF(day, cdate, monthEnd) / 36500, 2) AS SI
FROM
cte_Alldates
END
答案 2 :(得分:0)
我认为您需要以json:
的形式接收控制器中的数据$request->json('field_of_interest')
答案 3 :(得分:0)
问题是你的formData变量。而不是:
var formData = {
form: $("#newCustomerForm").serialize()
}
应该是
var formData=$("#newCustomerForm").serialize();