我的控制器中有一个函数,该函数将数组作为输入。该值从ajax传递。目前无法正确解码。
removeItem = (title, body){
let myArray = [
{ title : 'title 1', body: 'body of title one' },
{ title : 'title 2', body: 'body of title two' },
{ title : 'title 3', body: 'body of title three' },
]
//the problem is down here
let filteredArray = myArray.filter(item => {
item.title != title || item.body != body
}
// at this point i assume that the filtered array will not
// include the item that i want to remove
// so down here i reset the value of my original array to the filtered one
myArray = filteredArray
js:
/**
* @Route("/userLogin/{params}", name="userLogin", methods={"POST"})
* @param UserdbRepository $repository
* @param $params
* @return \Symfony\Component\HttpFoundation\Response
*/
public function userLogin(UserdbRepository $repository, $params) {
$email = $params[0];
$pass = $params[1];
print_r($params); // output correct value test@test.com
echo $params[0]; // output t
echo $email; // output t
.... rest of code
}
答案 0 :(得分:1)
使用Request
组件Symfony\Component\HttpFoundation\Request
,您可以自动为其接线:public function userLogin(UserdbRepository $repository, Request $request)
示例1:从requestBody
获取参数:
$email = $request->request->get('email', null);
如果请求中未提供$email
参数,则null
将是'email'
不要忘记在ajax请求中传递data
属性
const requestBody = {
'email': 'asd@example.com',
'pass': '123',
};
$.ajax({
url: `/userLogin/`,
type: "post",
data: requestBody,
});
注意:请勿以json
格式发送数据。您也可以从@Route
模式和$params
参数中删除{params}。
示例2:从'email'
获取queryString
参数:
$email = $request->query->get('email', null);
https://symfony.com/doc/current/components/http_foundation.html