i am trying to store a simple data (file title) in a div element into Mysql db.. can any one point out the error in my code? here's my scipt:
$( document ).ready(function() {
$( "#store-button" ).click(function() {
var title = $( "#pdf-title" ).text();
storeData(title);
});
});
function storeData(title)
{
$.ajax({
type: "POST",
url: '{{ path('store') }}',
data: { title: title } ,
success: function (data) {
// this is executed when ajax call finished well
alert('content of the executed page: ' + data);
},
error:function(error){
alert('Error: data couldnt be created');
console.log(error);
}
});
}
and here's my controller:
/**
* @Route("/store", name="store")
* @Method({"GET", "POST"})
*/
public function storeAction(Request $request)
{
$data = new Data();
$data->setFileName($request->request->get("title"));
$em = $this->getDoctrine()->getManager();
$em->persist($data);
$em->flush();
}
json data passed through jquery normally needs to be stringified, So you can try to do this:
function storeData(title)
{
var data = {title: title};
$.ajax({
type: "POST",
url: '{{ path('store') }}',
data: JSON.stringify(data),
success: function (data) {
// this is executed when ajax call finished well
alert('content of the executed page: ' + data);
},
error:function(error){
alert('Error: data couldnt be created');
console.log(error);
}
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.