我正在尝试将FSON从Twig模板发送到PHP类,这将保存在数据库中,但我记得404错误。
这是我的树枝模板的内容。我的目标是将此注释发送给data
var,但出于测试目的,我将其替换掉了。
function saveUser()
{
var e = document.getElementById("subject_select");
var subject = e.options[e.selectedIndex].text;
console.log(subject);
var e2 = document.getElementById("mark_select");
var mark = e2.options[e2.selectedIndex].value;
console.log(mark);
var e3 = document.getElementById("student_select");
var student = e3.options[e3.selectedIndex].value;
console.log(student);
//var data = ({ subject:subject, mark:mark, student:student}).serialize();
$.ajax({
type: "POST",
url: 'save/',
data: {subject:"a", mark:1, student:2},
success: function(response)
{
console.log(response);
}
});
和php中的类:
class TestMark extends Controller
{
/**
* @Route("/save/", name="_save")
*/
public function saveAction(){
$mysqli = new mysqli("localhost", "root", null, "school");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if (!$this->getRequest()->isXmlHttpRequest())
{//no ajax request, no play...
$this->redirect(
$this->generateUrl('_gradebook')
);
}
$jsonString = $this->getRequest();
$jsonArray = json_decode($jsonString, true);
/* create a prepared statement */
if ($stmt = $mysqli->prepare('INSERT INTO marks (studentId, mark, subject) VALUES (?,?,?)')) {
/* bind parameters for markers */
$stmt->bind_param("ssss", $jsonArray['subject'], $jsonArray['mark'], $jsonArray['student']);
/* execute query */
$stmt->execute();
/* close statement */
$stmt->close();
}
/* close connection */
$mysqli->close();
}
}
更新
经过一些更改后,我从Chrome控制台获取此类日志时出现错误500:
GET http://host/save/ 500 (Internal Server Error)
l.cors.b.crossDomain.send @ jquery.min.js:4
n.extend.ajax @ jquery.min.js:4
n.(anonymous function) @ jquery.min.js:4
saveUser @ gradebook:66
onclick @ gradebook:125
JS和ajax:
<input type="hidden" value="{{ path('_save') }}" id="save_url">
<script>
function saveUser()
{
var requestUrl = $('#save_url').val();
var e = document.getElementById("subject_select");
var subject = e.options[e.selectedIndex].text;
console.log(subject);
var e2 = document.getElementById("mark_select");
var mark = e2.options[e2.selectedIndex].value;
console.log(mark);
var e3 = document.getElementById("student_select");
var student = e3.options[e3.selectedIndex].value;
//var data = ({ subject:subject, mark:mark, student:student }).serialize();
console.log(student);
$.post('{{path('AppBundle_ajax_update')}}',
{subject: 'mydata1', mark:'mydata2', student:'mydata3'},
function(response){
if(response.code == 100 && response.success){//dummy check
//do something
}
}, "json");
}
MarkController.php:
class MarkController extends Controller
{
/**
* @Route("/save/", name="_save")
*/
public function saveAction(){
$request = $this->container->get('request');
$mark = new Mark();
$mark->setSubject($request->query->get('subject'));
$mark->setMark($request->query->get('mark'));
$mark->setStudent($request->query->get('student'));
$em = $this->getDoctrine()->getManager();
$em->persist($mark);
$em->flush();
$response = array("code" => 100, "success" => true);
return new Response(json_encode($response));
}
}
答案 0 :(得分:0)
注意:重命名控制器类
TestMark
以强>
TestMarkController
答案 1 :(得分:0)
您的控制器没有返回任何回复。尝试返回:
return new Symfony\Component\HttpFoundation\Response('student saved');
关闭mysql连接后。
我还建议在树枝模板上获取请求网址。像这样:
<input type="hidden" value="{{ path('_save') }}" id="save_url">
然后,在你的js:
var requestUrl = $('#save_url').val();
$.ajax({
...
url: requestUrl,
...
});
根据Imanali Mamadiev的建议,您应该重命名控制器类。